//不同类型的任务类型，使用不同的图层
//不同的房屋类型，使用不同的图层
//
define([
    "UtilDir/util",
    "UtilDir/tool",
    "UtilDir/combotree"
],function(Util,Tool,Combotree) {
    var fwsjcjMap;
    var fwsjcjCurrentAjax;
    var fwsjcjTasks = [];
    var fwsjcjFWInfos = [];

    var localProvinceRounds;
    var getDivisionController = function () {
        return getServer() + "/platform/org/divisions/";
    };
///////////////////////////////////////////////////////////////////////////天地图工具箱功能
//标注
    var fwsjcjMarkPolygonPoints = [];//标注面的点坐标数组
    var fwsjcjMarkLabels = [];//标注文字
    var fwsjcjMarkMapClick;//标注地图点击事件
    var fwsjcjMarkPolygonTool;//标注面绘制工具
//绘制
    var fwsjcjDrawMarkerTool;//绘制点图标工具
    var fwsjcjDrawPointMarkers = [];//绘制点图标数组
    var fwsjcjDrawLineTool;//绘制线工具
    var fwsjcjDrawLines = [];//绘制线数组

    var polygonTaskTool;//任务区域
    var taskPolygon;
    var PolygonPoints = [];
    var taskAreas;

    var colors = ["blue", "red", "yellow", "DarkOrage", "Lime", "Teal", "Fuchsia", "Green", "Black", "SpringGreen"];

    var fwsjcjDrawPolygonTool;//绘制多边形工具
    var fwsjcjDrawPolygons = [];//绘制多边形数组
    var fwsjcjDrawRectTool;//绘制长方形工具
    var fwsjcjDrawRects = [];//绘制长方形数组
    var fwsjcjDrawCircleTool;//绘制圆工具
    var fwsjcjDrawCircles = [];//绘制圆数组
//选择功能
    var fwsjcjSelectDeleteBool = false;//是否删除选择的已绘制图形的参数
//测量
    var fwsjcjMeasureLineTool;//测距工具
    var fwsjcjMeasurePolygonTool;//测面工具
//定位
    var fwsjcjPositionMarkers = [];//定位图标数组

    var sysPath =  getServer() + "/static/app/fwsjcj";
    var TaskSetFormHtml = sysPath + "/taskmanage/views/taskSetForm.html";
    var FWInfoFormHtml = sysPath + "/taskmanage/views/fwBaseInfoForm.html";
    //var getZwController = function(){
    //    return getServer() + "/platform/org/zws/";
    //};
    var getZwController = function(){
        return getServer() + "/fw/taskForm";
    };

//初始化
    function fwsjcjInitAll() {

        //初始化天地图及切换地图事件
        fwsjcjMap = initTMap(fwsjcjMap, 'fwsjcj-map', "#fwsjcj");
        if (fwsjcjMap != null) {//初始化地图成功
            getLocalUserBelongProvince();
            //初始化图层数组
            //fwsjcjInitLayerArray();
            //初始化事件
            fwsjcjInitEvent();
            //初始化地图工具
            fwsjcjInitMapTools();
            //显示图例
            displayLegend();
            //获取所有任务
            fwsjcjTaskInitQuery();
            //获取所有房屋信息
            fwsjcjFWInfoInitQuery();
            //添加浏览器的窗口调整地图页面大小事件
            initDivZindex("#fwsjcj");
            //禁用浏览器右键菜单
            initTMapContextMenu();

            taskLeftTitleSwitch();

            //initChinaRegion();
            //initChinaProvinceRegion();

            //fwsjcjMap.addEventListener("dragend", displayRoundsFWDatas);
            //fwsjcjMap.addEventListener("zoomend", displayRoundsFWDatas);
            TEvent.addListener(fwsjcjMap,"moveend",displayRoundsFWDatas);
            TEvent.addListener(fwsjcjMap,"zoomend",displayRoundsFWDatas);

            initAlert("#fwsjcj","初始化成功！",true);
        } else {
            //地图初始化失败
            initAlert("#fwsjcj", "地图初始化失败", false);
        }
    }

    function getLocalUserBelongProvince() {
        localProvinceRounds=null;
        $.ajax({
            type: "GET",
            url: "/fw/user/getAddre",
            dataType: "json",
            success: function (data) {
                if(data!=null&&data!="中华人民共和国"){
                    var provinceRegion=initGetAreaInfosArray(data);
                    localProvinceRounds = provinceRegion[3];
                    fwsjcjMap.centerAndZoom(new TLngLat(provinceRegion[0], provinceRegion[1]), provinceRegion[2]);
                    if(localProvinceRounds!=null && localProvinceRounds.length>0){
                        for(var i=0;i<localProvinceRounds.length;i++) {
                            var regionLngLats = [];
                            var regionArr = localProvinceRounds[i].split(",");
                            if (regionArr != null && regionArr.length > 0) {
                                for (var j = 0; j < regionArr.length; j++) {
                                    var lnglatArr = regionArr[j].split(" ");
                                    var lnglat = new TLngLat(lnglatArr[0], lnglatArr[1]);
                                    regionLngLats.push(lnglat);
                                }
                            }
                            //创建面对象
                            var polygon = new TPolygon(regionLngLats, {
                                strokeColor: "#6495ED",
                                strokeWeight: 3,
                                strokeOpacity: 1,
                                fillColor: "#6495ED",
                                fillOpacity: 0
                            });
                            //向地图上添加面
                            fwsjcjMap.addOverLay(polygon);
                        }
                    }
                }else{}
            }
        });
    }

    function initChinaProvinceRegion() {
        var allAreaArray = initGetAreaArray();
        for(var m = 0;m<allAreaArray.length;m++){
            var provinceName=allAreaArray[m];
            var provinceRegion=initGetAreaInfosArray(provinceName);

            var pointsArray = provinceRegion[3];
            //显示省市行政区域边界线
            if(pointsArray!=null && pointsArray.length>0){
                for(var i=0;i<pointsArray.length;i++) {
                    var regionLngLats = [];
                    var regionArr = pointsArray[i].split(",");
                    if (regionArr != null && regionArr.length > 0) {
                        for (var j = 0; j < regionArr.length; j++) {
                            var lnglatArr = regionArr[j].split(" ");
                            var lnglat = new TLngLat(lnglatArr[0], lnglatArr[1]);
                            regionLngLats.push(lnglat);
                        }
                    }
                    //创建面对象
                    var polygon = new TPolygon(regionLngLats, {
                        strokeColor: "#6495ED",
                        strokeWeight: 1,
                        strokeOpacity: 1,
                        fillColor: "#6495ED",
                        fillOpacity: 0
                    });
                    //向地图上添加面
                    fwsjcjMap.addOverLay(polygon);
                }
            }
        }
    }

    function initChinaRegion(){
        var points=createChinaMapLine();
        var polygon = new TPolygon(points, {
            strokeColor: "red",
            strokeWeight: "2px",
            //strokeWeight: 2,
            strokeOpacity: 1,
            fillOpacity: 0.2
        });
        //向地图上添加面
        fwsjcjMap.addOverLay(polygon);

        var points = polygon.getLngLats();
    }

    function initOperateData(){
        //fwsjcjMap.addOverLay(polygon);
        fwsjcjTasks = [];
        fwsjcjTasks.length=0;
        fwsjcjFWInfos = [];
        fwsjcjFWInfos.length=0;
        PolygonPoints=[];
        PolygonPoints.length=0;
        fwsjcjMap.clearOverLays();
        //获取所有任务
        fwsjcjTaskInitQuery();
        //获取所有房屋信息
        fwsjcjFWInfoInitQuery();

        taskLeftTitleSwitch();
    }

    function displayLegend() {
        $("#fwsjcj-fwlegend").show();
    }

    function initfwsjcjTMapAddMarkLabelArray(map,fwinfo,point,name, icon) {
        if (point != null && icon != null) {
            //有图标地址
            //根据经纬度、文本、图标icon，建立图标
            var marker = new TMarker(new TLngLat(point[0], point[1]), {icon: icon});
            TEvent.addListener(marker, "click", function (p,btn) {
                if(fwinfo!=null) {
                    $("#fwsjcj-contextMenuDiv").hide();
                    $("#fwsjcjEx-contextMenuDiv").hide();

                    if (btn == 2) {
                        initShowContextMenu("#fwsjcj", 3, p[0], p[1]);
                        fwsjcjCreateFWContextMenu(fwinfo);
                    }
                }
            });
            //判断是否有任务名称
            if (name != null) {
                var title = name;
                marker.setTitle(title);
            } else {
                marker.setTitle("未命名");
            }
            //添加图标
            map.addOverLay(marker);
            fwmarkers.push(marker);

            var label = initTMapAddMarkText(point, name, -20, "black");
            //添加文字
            map.addOverLay(label);
            fwlabels.push(label);

        }
    }

    /*function initTMapAddMarkLabelArray(fwinfo, icon) {
        if (fwinfo.point != null && icon != null) {
            //有图标地址
            //根据经纬度、文本、图标icon，建立图标
            var marker = new TMarker(new TLngLat(fwinfo.point[0], fwinfo.point[1]), {icon: icon});
            TEvent.addListener(marker, "click", function (p,btn) {
                $("#fwsjcj-contextMenuDiv").hide();
                $("#fwsjcjEx-contextMenuDiv").hide();

                if(btn==2) {
                    initShowContextMenu("#fwsjcj", 3, p[0], p[1]);
                    fwsjcjCreateFWContextMenu(fwinfo);
                }
            });
            //判断是否有任务名称
            if (fwinfo.name != null) {
                var title = fwinfo.name;
                marker.setTitle(title);
            } else {
                marker.setTitle("未命名");
            }
            //添加图标
            fwsjcjMap.addOverLay(marker);

            var label = initTMapAddMarkText(fwinfo.point, fwinfo.name, -20, "black");
            //添加文字
            fwsjcjMap.addOverLay(label);

        }
    }

    function initTMapAddMarkText(lonlat, imgText, yOffset, fontColor) {
        //新建图标文字
        var configText = {
            offset: new TPixel(-25, yOffset),
            position: new TLngLat(lonlat[0], lonlat[1])
        };
        var label = new TLabel(configText);
        label.setLabel(imgText);//文本内容
        label.setTitle(imgText);//提示信息
        label.setBackgroundColor("white");//背景色
        label.setBorderColor("gray");//边框颜色
        label.setBorderLine(1);//边框线宽
        label.setNoWrap(true);//允许换行
        label.setFontSize(12);//文本字体大小
        label.setFontColor(fontColor);//文本字体颜色
        label.setOpacity(1);//设置透明度

        return label;
    }*/


    var fwmarkers=[];
    var fwlabels=[];
//添加房屋信息
    function drawFWInfos() {
        fwmarkers=[];
        fwmarkers.length=0;
        fwlabels=[];
        fwlabels.length=0;
        for (var i = 0; i < fwsjcjFWInfos.length; i++) {
            var info = fwsjcjFWInfos[i];
            var icon = initTMapGetIconByType(info.buildtype,info.sptype);
            initfwsjcjTMapAddMarkLabelArray(fwsjcjMap,info,info.point,info.name, icon);
        }
    }

    function drawFWInfosBak(fws) {
        fwmarkers=[];
        fwmarkers.length=0;
        fwlabels=[];
        fwlabels.length=0;
        for (var i = 0; i < fws.length; i++) {
            var info = fws[i];
            var icon = initTMapGetIconByType(info.buildtype,info.sptype);
            initfwsjcjTMapAddMarkLabelArray(fwsjcjMap,info,info.point,info.name, icon);
        }
    }

    function clearFWInfos(){
        for(var i=0;i<fwmarkers.length;i++){
            fwsjcjMap.removeOverLay(fwmarkers[i]);
        }
        for(var i=0;i<fwlabels.length;i++){
            fwsjcjMap.removeOverLay(fwlabels[i]);
        }
        fwmarkers=[];
        fwmarkers.length=0;
        fwlabels=[];
        fwlabels.length=0;
    }

//获取所有任务
    function fwsjcjTaskInitQuery() {
        fwsjcjCurrentAjax = $.ajax({
            type: "GET",
            url: "/fw/task/getAllTask",
            dataType: "json",
            success: function (data) {
                fwsjcjTasks = [];
                fwsjcjTasks.length = 0;
                for (var i = 0; i < data.length; i++) {
                    var res = data[i];
                    if(res.delFlag=="0") {
                        var task = new fwsjcjTask(res.taskId, res.taskNum, res.taskName, res.mapInfo, res.status, res.taskType, res.groupId, res.groupName, res.groupLeaderId, res.groupLeader,res.taskArea,res.division);
                        fwsjcjTasks.push(task);
                    }
                }
                drawPolygonByPoint();
            }
        });
    }

//获取所有房屋信息
    function fwsjcjFWInfoInitQuery() {
        fwsjcjCurrentAjax = $.ajax({
            type: "GET",
            url: "/fw/taskForm/queryAll",
            dataType: "json",
            success: function (data) {
                fwsjcjFWInfos = [];
                fwsjcjFWInfos.length = 0;
                //console.log(data);
                //debugger;
                for (var i = 0; i < data.length; i++) {
                    var res = data[i];
                    var info = new fwsjcjFWInfo(res.fwId, res.taskId,res.taskName, res.bulidingLongitude, res.bulidingLatitude, res.buildingName,res.formId, res.createTime, res.spType, res.bulidingType);
                    fwsjcjFWInfos.push(info);
                    //console.log(info);
                }
                //displayRoundsFWDatas();

            }
        });
    }

    function displayRoundsFWDatas(){
        if(displayFwData) {
            var zoom = fwsjcjMap.getZoom();
            var bounds = fwsjcjMap.getBounds();
            var minlon = bounds.XminNTU;
            var maxlon = bounds.XmaxNTU;
            var minlat = bounds.YminNTU;
            var maxlat = bounds.YmaxNTU;
            var displayDatas = [];
            var theSuitableDatas = [];
            var taskIds = [];
            for (var i = 0; i < fwsjcjFWInfos.length; i++) {
                var lon = fwsjcjFWInfos[i].lon;
                var lat = fwsjcjFWInfos[i].lat;
                if (judgeThePointInRectangle(lon, lat, minlon, maxlon, minlat, maxlat)) {
                    var taskIdBak=null;
                    if(taskIds.length>0) {
                        taskIdBak=taskIds[taskIds.length - 1];
                    }

                    var taskId =fwsjcjFWInfos[i].taskid;
                    if(taskId!=taskIdBak) {
                        taskIds.push(taskId);
                    }


                    theSuitableDatas.push(fwsjcjFWInfos[i]);
                }
            }
            if (theSuitableDatas.length > 0) {
                var taskLength=taskIds.length;
                var count = 100000;
                if (zoom <= 5) {
                    count = 1;
                } else if (zoom > 5 && zoom <= 6) {
                    if(taskLength<100)
                        count = 3;
                    else{
                        count=1;
                    }
                } else if (zoom > 6 && zoom <= 7) {
                    if(taskLength<50) {
                        count = 5;
                    }else if(taskLength>=50&&taskLength<100){
                        count = 3;
                    }
                    else{
                        count=1;
                    }
                } else if (zoom > 7 && zoom <= 8) {
                    if(taskLength<30) {
                        count = 10;
                    }else if(taskLength>=30&&taskLength<50){
                        count = 5;
                    }
                    else{
                        count=1;
                    }
                } else if (zoom > 8 && zoom <= 9) {
                    if(taskLength<20) {
                        count = 15;
                    }else if(taskLength>=20&&taskLength<30){
                        count = 10;
                    }else if(taskLength>=30&&taskLength<50){
                        count = 5;
                    }else{
                        count=1;
                    }
                } else if (zoom > 9 && zoom <= 10) {
                    if(taskLength<10) {
                        count = 20;
                    }else if(taskLength>=10&&taskLength<20){
                        count = 10;
                    }else if(taskLength>=20&&taskLength<50){
                        count = 5;
                    }else{
                        count=1;
                    }
                } else if (zoom > 10 && zoom <= 12) {
                    if(taskLength<10) {
                        count = 30;
                    }else if(taskLength>=10&&taskLength<20){
                        count = 10;
                    }else if(taskLength>=20&&taskLength<50){
                        count = 5;
                    }else{
                        count=1;
                    }
                } else if (zoom > 10 && zoom <= 12) {
                    if(taskLength<5) {
                        count = 45;
                    }else if(taskLength>=5&&taskLength<10){
                        count = 30;
                    }else if(taskLength>=10&&taskLength<20){
                        count = 10;
                    }else if(taskLength>=20&&taskLength<60){
                        count = 5;
                    }else{
                        count=1;
                    }
                } else if (zoom > 12 && zoom <= 14) {
                    if(taskLength<5) {
                        count = 60;
                    }else if(taskLength>=5&&taskLength<15){
                        count = 20;
                    }else if(taskLength>=15&&taskLength<25){
                        count = 10;
                    }else if(taskLength>=25&&taskLength<50){
                        count = 5;
                    }else{
                        count=1;
                    }
                } else if (zoom > 14 && zoom <= 16) {
                    if(taskLength<3) {
                        count = 80;
                    }else if(taskLength>=3&&taskLength<8){
                        count = 30;
                    }else if(taskLength>=8&&taskLength<20){
                        count = 20;
                    }else if(taskLength>=20&&taskLength<30){
                        count = 10;
                    }else if(taskLength>=30&&taskLength<50){
                        count = 5;
                    }else{
                        count=1;
                    }
                }
                console.log(count);
                console.log(taskLength);
                debugger;
                if (theSuitableDatas.length > count) {
                    var needs = [];
                    var taskId = theSuitableDatas[0].taskid;
                    displayDatas.push(theSuitableDatas[0]);
                    needs.push(theSuitableDatas[0]);
                    for (var i = 1; i < theSuitableDatas.length; i++) {
                        var taskId1 = theSuitableDatas[i].taskid;
                        if (taskId == taskId1) {
                            if (needs.length < count) {
                                displayDatas.push(theSuitableDatas[i]);
                                needs.push(theSuitableDatas[i]);
                            } else {

                            }
                        } else {
                            needs = [];
                            needs.length = 0;
                            displayDatas.push(theSuitableDatas[i]);
                            needs.push(theSuitableDatas[i]);
                            taskId = taskId1;
                        }
                    }
                } else {
                    displayDatas = theSuitableDatas;
                }
                //清空房屋
                clearFWInfos();
                //增加房屋
                drawFWInfosBak(displayDatas);
            }
        }
    }

    function judgeThePointInRectangle(lon,lat,minlon,maxlon,minlat,maxlat){
        if(lon>=minlon&&lon<=maxlon&&lat>=minlat&&lat<=maxlat){
            return true;
        }
        return false;
    }

    var displayFwData=false;
    function displayFWDatas(){
        if ($("#fwsjcj-fwDisplay img").attr("title") == "显示房屋") {
            $("#fwsjcj-fwDisplay img").attr("title", "隐藏房屋");
            $("#fwsjcj-fwDisplay").children("span").html("隐藏房屋");
            $("#fwsjcj-fwDisplay img").attr("src", "/static/app/fwsjcj/taskmanage/image/fwhide.png");
            displayFwData=true;
            displayRoundsFWDatas();
        }else {
            $("#fwsjcj-fwDisplay img").attr("title", "显示房屋");
            $("#fwsjcj-fwDisplay").children("span").html("显示房屋");
            $("#fwsjcj-fwDisplay img").attr("src", "/static/app/fwsjcj/taskmanage/image/fwdisplay.png");
            displayFwData=false;
            clearFWInfos();
        }
    }

//浏览器的窗口改变、地图模块切换调整地图页面大小事件
    function initTMapResizeWindow() {
        $(window).on("resize", function () {
            //全屏设置功能
            initTMapFullScreenMapDiv();
            //初始化DIV层叠顺序，设置Div位置居中
            initDivZindex("#fwsjcj");
        });
    }

//禁用浏览器右键菜单
    function initTMapContextMenu() {
        //禁用浏览器右键菜单
        document.oncontextmenu = function () {
            return false;
        };
    }

//控制全屏功能
    function initTMapFullScreenMapDiv() {

        if ($(".init-fullScreenMapDiv img").attr("title") == "全屏显示") {//全屏显示
            $(".init-fullScreenMapDiv img").attr("title", "退出全屏");
            $(".init-fullScreenMapDiv img").attr("src", "/static/app/fwsjcj/taskmanage/image/gn-r.png");
            //设置文字颜色
            $(".init-fullScreenMapDiv span").css("color", "#00a7ff");
            //全屏显示
            initTMapFullScreen();

        } else if ($(".init-fullScreenMapDiv img").attr("title") == "退出全屏") {//退出全屏
            $(".init-fullScreenMapDiv img").attr("title", "全屏显示");
            $(".init-fullScreenMapDiv img").attr("src", "/static/app/fwsjcj/taskmanage/image/gn.png");
            //设置文字颜色
            $(".init-fullScreenMapDiv span").css("color", "#fff");
            //退出全屏功能
            initTMapExitFullScreen();
        }
    }

//全屏显示功能
    function initTMapFullScreen() {
        var el = document.documentElement;
        var rfs = el.requestFullScreen || el.webkitRequestFullScreen || el.mozRequestFullScreen || el.msRequestFullScreen;
        if (typeof rfs != "undefined" && rfs) {
            rfs.call(el);
        } else if (typeof window.ActiveXObject != "undefined") {
            //F11全屏
            var wscript = new ActiveXObject("WScript.Shell");
            if (wscript != null) {
                wscript.SendKeys("{F11}");
            }
        }
    }

//退出全屏功能
    function initTMapExitFullScreen() {
        var rfs = document.cancelFullScreen || document.webkitCancelFullScreen || document.mozCancelFullScreen || document.msCancelFullScreen;
        if (typeof rfs != "undefined" && rfs) {
            if (document.cancelFullScreen) {
                document.cancelFullScreen();
            } else if (document.mozCancelFullScreen) {
                document.mozCancelFullScreen();
            } else if (document.webkitCancelFullScreen) {
                document.webkitCancelFullScreen();
            } else if (document.msCancelFullScreen) {
                document.msCancelFullScreen();
            }
        } else if (typeof window.ActiveXObject != "undefined") {
            //F11全屏
            var wscript = new ActiveXObject("WScript.Shell");
            if (wscript != null) {
                wscript.SendKeys("{F11}");
            }
        }
    }

    function initQueryAlert(str, data) {
        //设置信息框显示文字
        $(str + "-stopQueryDiv p").html("<img width='30px;' src='/static/app/fwsjcj/taskmanage/image/ajax-loading.gif'><br/><br/>" + data);
        //显示信息窗
        $(str + "-stopQueryDiv").show();
        //设置DIV层叠顺序为最大
        initSetDivZindexMax(str + "-stopQueryDiv");
        if (data == "初始化...") {
            //重新设置地图模块样式
        }
    }

//地图缩放到权限位置
    function fwsjcjZoomDefaultPosition() {
        //获取权限区域选择列表
        //fwsjcjGetSearchAreaSelectList();
        taskLeftTitleSwitch();
        //var LonLat = [113.2, 25.4];
        var LonLat  = [103.2, 34.4];
        //地图缩放级别
        var zoom = 4;
        fwsjcjMap.centerAndZoom(new TLngLat(LonLat[0], LonLat[1]), zoom);

        //设置主界面图标文字
        initSetZoomDefaultPosition(true, "#fwsjcj");
        //取消跟踪显示
        //fwsjcjStopTraceCenter();
    }

    function initSetZoomDefaultPosition(bool, str) {
        if (bool) {//返回主界面
            //设置主界面图标文字
            $(str + "-ZoomDefaultPosition img").attr("src", "/static/app/fwsjcj/taskmanage/image/home-r.png");
            $(str + "-ZoomDefaultPosition span").css("color", "#00a7ff");
        } else {
            //设置主界面图标文字
            $(str + "-ZoomDefaultPosition img").attr("src", "/static/app/fwsjcj/taskmanage/image/home.png");
            $(str + "-ZoomDefaultPosition span").css("color", "#fff");
        }
    }

//打开/关闭工具箱
    function initSetToolsBoxDiv(str) {
        //str:"#ycjk"
        taskLeftTitleSwitch();
        var title = $(str + "-setToolsBoxDiv img").attr("title");

        //打开工具箱
        if ($(str + "-setToolsBoxDiv img").attr("title") == "打开工具箱") {
            //显示工具箱
            $(str + "-ToolsBoxDiv").show();
            //设置图标显示
            $(str + "-setToolsBoxDiv img").attr("title", "关闭工具箱");
            $(str + "-setToolsBoxDiv img").attr("src", "/static/app/fwsjcj/taskmanage/image/tools-r.png");
            //设置文字颜色
            $(str + "-setToolsBoxDiv span").css("color", "#00a7ff");
            //设置工具箱的图标文字颜色为默认
            initSetToolDivImg(str);

        } else {//关闭工具箱
            //隐藏工具箱
            $(str + "-ToolsBoxDiv").hide();
            //隐藏所有工具栏
            $(str + "-AllToolsDiv div").hide();
            //隐藏搜索结果面板
            $(str + "-searchResultDiv").hide();
            //设置图标显示
            $(str + "-setToolsBoxDiv img").attr("title", "打开工具箱");
            $(str + "-setToolsBoxDiv img").attr("src", "/static/app/fwsjcj/taskmanage/image/tools.png");
            //设置文字颜色
            $(str + "-setToolsBoxDiv span").css("color", "#fff");
        }

    }

//设置工具箱的图标文字颜色为默认
    function initSetToolDivImg(str) {
        //设置工具箱图标显示
        $(str + "-ToolsBoxDiv p span").css("color", "#fff");
        $(str + "-huizhi img").attr("src", "/static/app/fwsjcj/taskmanage/image/huiz.png");
        $(str + "-celiang img").attr("src", "/static/app/fwsjcj/taskmanage/image/cel.png");
        $(str + "-dingwei img").attr("src", "/static/app/fwsjcj/taskmanage/image/dingw.png");
        $(str + "-daochu img").attr("src", "/static/app/fwsjcj/taskmanage/image/daoc.png");
        $(str + "-biaozhu img").attr("src", "/static/app/fwsjcj/taskmanage/image/biaoz.png");

    }

//标注工具
    function initMarkPopupTool(str) {
        //str:"#ycjk"
        /*//设置DIV层叠顺序为最大
    initSetDivZindexMax(str+"-MarkPopupToolsDiv");*/
        //隐藏所有工具栏
        $(str + "-AllToolsDiv div").hide();
        //隐藏搜索结果面板
        $(str + "-searchResultDiv").hide();
        if ($(str + "-biaozhu img").attr("src") == "/static/app/fwsjcj/taskmanage/image/biaoz.png") {
            //设置工具箱的图标文字颜色为默认
            initSetToolDivImg(str);
            //设置图标显示
            $(str + "-biaozhu img").attr("src", "/static/app/fwsjcj/taskmanage/image/biaoz-r.png");
            $(str + "-biaozhu span").css("color", "#00a7ff");
            //显示标注工具栏
            $(str + "-MarkPopupToolsDiv").show();
        } else {
            //设置工具箱的图标文字颜色为默认
            initSetToolDivImg(str);
        }

    }

//绘制工具
    function initDrawTool(str) {
        //隐藏所有工具栏
        $(str + "-AllToolsDiv div").hide();
        //隐藏搜索结果面板
        $(str + "-searchResultDiv").hide();

        if ($(str + "-huizhi img").attr("src") == "/static/app/fwsjcj/taskmanage/image/huiz.png") {
            //设置工具箱的图标文字颜色为默认
            initSetToolDivImg(str);
            //设置图标显示
            $(str + "-huizhi img").attr("src", "/static/app/fwsjcj/taskmanage/image/huiz-r.png");
            $(str + "-huizhi span").css("color", "#00a7ff");
            //显示绘制工具栏
            $(str + "-DrawToolsDiv").show();
        } else {
            //设置工具箱的图标文字颜色为默认
            initSetToolDivImg(str);
        }

    }

//测量工具
    function initMeasureTool(str) {
        //隐藏所有工具栏
        $(str + "-AllToolsDiv div").hide();
        //隐藏搜索结果面板
        $(str + "-searchResultDiv").hide();

        if ($(str + "-celiang img").attr("src") == "/static/app/fwsjcj/taskmanage/image/cel.png") {
            //设置工具箱的图标文字颜色为默认
            initSetToolDivImg(str);
            //设置图标显示
            $(str + "-celiang img").attr("src", "/static/app/fwsjcj/taskmanage/image/cel-r.png");
            $(str + "-celiang span").css("color", "#00a7ff");
            //显示测量工具栏
            $(str + "-MeasureToolsDiv").show();
        } else {
            //设置工具箱的图标文字颜色为默认
            initSetToolDivImg(str);
        }

    }

//定位工具
    function initMousePositionTool(str) {
        //隐藏所有工具栏
        $(str + "-AllToolsDiv div").hide();
        //隐藏搜索结果面板
        $(str + "-searchResultDiv").hide();

        if ($(str + "-dingwei img").attr("src") == "/static/app/fwsjcj/taskmanage/image/dingw.png") {
            //设置工具箱的图标文字颜色为默认
            initSetToolDivImg(str);
            //清空经纬度文本框
            $(str + "-MousePositionLon").val("");
            $(str + "-MousePositionLat").val("");
            //设置图标显示
            $(str + "-dingwei img").attr("src", "/static/app/fwsjcj/taskmanage/image/dingw-r.png");
            $(str + "-dingwei span").css("color", "#00a7ff");
            //显示定位工具栏
            $(str + "-MousePositionDiv").show();
        } else {
            //设置工具箱的图标文字颜色为默认
            initSetToolDivImg(str);
        }

    }

//设置标注工具栏的图标文字颜色为默认
    function initSetMarkToolDivImg(str) {
        $(str + "-MarkPopupToolsDiv span").css("color", "#fff");
        $(str + "-MarkPopupBtn img").attr("src", "/static/app/fwsjcj/taskmanage/image/biaoz.png");//开始标注
        $(str + "-bz_qxbz img").attr("src", "/static/app/fwsjcj/taskmanage/image/qxbiaoz.png");//取消标注
        $(str + "-bz_qkjg img").attr("src", "/static/app/fwsjcj/taskmanage/image/qux.png");//清空结果

    }

//设置绘制工具栏的图标文字颜色为默认
    function initSetDrawToolDivImg(str) {
        $(str + "-DrawToolsDiv span").css("color", "#fff");
        $(str + "-hz_dian img").attr("src", "/static/app/fwsjcj/taskmanage/image/dian.png");//点
        $(str + "-hz_zx img").attr("src", "/static/app/fwsjcj/taskmanage/image/xian.png");//线
        $(str + "-hz_dbx img").attr("src", "/static/app/fwsjcj/taskmanage/image/dbx.png");//多边形
        $(str + "-hz_cfx img").attr("src", "/static/app/fwsjcj/taskmanage/image/cfx.png");//长方形
        $(str + "-hz_yuan img").attr("src", "/static/app/fwsjcj/taskmanage/image/yuan.png");//圆
        $(str + "-hz_jshz img").attr("src", "/static/app/fwsjcj/taskmanage/image/jshz.png");//结束绘制
        $(str + "-SelectDrawBtn img").attr("src", "/static/app/fwsjcj/taskmanage/image/kqxz.png");//开启删除
        $(str + "-hz_qk img").attr("src", "/static/app/fwsjcj/taskmanage/image/qux.png");//清空结果

    }

//设置测量工具栏的图标文字颜色为默认
    function initSetMeasureToolDivImg(str) {
        $(str + "-MeasureToolsDiv span").css("color", "#fff");
        $(str + "-cl_jl img").attr("src", "/static/app/fwsjcj/taskmanage/image/jul.png");//测距
        $(str + "-cl_mj img").attr("src", "/static/app/fwsjcj/taskmanage/image/mianj.png");//测面
        $(str + "-cl_jscl img").attr("src", "/static/app/fwsjcj/taskmanage/image/jshz.png");//结束测量
        $(str + "-cl_qk img").attr("src", "/static/app/fwsjcj/taskmanage/image/qux.png");//清空结果

    }

//设置定位工具栏的图标文字颜色为默认
    function initSetMousePositionDivImg(str) {
        $(str + "-MousePositionDiv span").css("color", "#fff");
        $(str + "-dw_qk img").attr("src", "/static/app/fwsjcj/taskmanage/image/qux.png");//清空结果
        $(str + "-dw_tj img").attr("src", "/static/app/fwsjcj/taskmanage/image/dingwd.png");//添加定位点

    }

//设置搜索工具栏的图标文字颜色为默认
    function initSetLocalSearchDivImg(str) {
        $(str + "-MousePositionDiv span").css("color", "#fff");
        $(str + "-ss_qk img").attr("src", "/static/app/fwsjcj/taskmanage/image/qux.png");//清空结果
        $(str + "-ss_ss img").attr("src", "/static/app/fwsjcj/taskmanage/image/chaxun1.png");//搜索

    }

//初始化DIV层叠顺序，设置Div位置居中
    function initDivZindex(str) {
        if ($(str + "-mapDiv").offset() != undefined) {//有地图页面信息
            var bodyWidth = window.innerWidth-15;//1920px
            var mapTop = $(str + "-mapDiv").offset().top;//142px
            var mapHeight =  window.innerHeight-40;//796px
            var mapWidth = bodyWidth;//1469px
            //设置地图宽度
            $(str + "-mapDiv").css("width", mapWidth + "px");
            //设置地图高度
            $(str + "-mapDiv").css("height", mapHeight + "px");
            //设置地图宽度
            $(str + "-map").css("width", mapWidth + "px");
            //设置地图高度
            $(str + "-map").css("height", mapHeight + "px");

            //设置鼠标经纬度位置Div位置
            $(str + "-mousemoveLngLatDiv").css("top", mapHeight - 50 + "px");

            //设置左侧控件栏位置
            $(str + "-setDivPosition").css("z-index", "1");
            //工具箱控件
            $(str + "-ToolsBoxDiv").css("z-index", "1");
            //工具箱栏
            $(str + "-AllToolsDiv div").css("z-index", "1");

            //消息弹窗
            $(str + "-infosDiv").css("z-index", "3");
            $(str + "-stopQueryDiv").css("z-index", "3");
            //设置消息窗的位置向上居中
            $(str + "-infosDiv").css("left", mapWidth / 2 - $(str + "-infosDiv").outerWidth() / 2 + "px");
            $(str + "-infosDiv").css("top", "100px");
            //设置查询消息框的位置向上居中
            $(str + "-stopQueryDiv").css("left", mapWidth / 2 - $(str + "-stopQueryDiv").outerWidth() / 2 + "px");
            $(str + "-stopQueryDiv").css("top", "100px");

            //右键菜单
            $(str + "-contextMenuDiv").css("z-index", "4");
            //隐藏右键菜单
            $(str + "-contextMenuDiv").hide();

            if(fwsjcjMap!=null) {
                fwsjcjMap.checkResize();
            }
        }
    }

//设置DIV层叠顺序为最大，设置Div位置居中
    function initSetDivZindexMax(id) {
        var strArray = id.split("-");
        if (strArray.length == 2) {
            var str = strArray[0];//str:"#ycjk"
            //初始化DIV层叠顺序，设置Div位置居中
            initDivZindex(str);
            //设置DIV的层叠顺序为最大
            $(id).css("z-index", "100");
        }
    }

//初始化地图工具
    function fwsjcjInitMapTools() {
        fwsjcjInitMarkTool();//初始化标注工具
        fwsjcjInitMeasureTool();//初始化测量工具
        fwsjcjInitDrawPoint();//初始化绘制点图标工具
        fwsjcjInitDrawPolyline();//初始化绘制线工具
        fwsjcjInitDrawPolygon();//初始化绘制多边形工具
        fwsjcjInitDrawRect();//初始化绘制长方形工具
        fwsjcjInitDrawCircle();//初始化绘制圆工具
        fwsjcjInitSelectTool();//初始化选择工具
    }

//初始化标注工具
    function fwsjcjInitMarkTool() {
        fwsjcjMarkLabels = [];//标注文字数组
        //标注面的样式
        var configPolygon = {
            strokeColor: "blue",	//折线颜色
            fillColor: "#FFFFFF",	//填充颜色。当参数为空时，折线覆盖物将没有填充效果
            strokeWeight: "3px",	//折线的宽度，以像素为单位
            strokeOpacity: 0.5,	//折线的透明度，取值范围0 - 1
            fillOpacity: 0.5,			//填充的透明度，取值范围0 - 1
            showLabel: false             //是否显示页面，默认为true.
        };
        //创建测面工具对象
        fwsjcjMarkPolygonTool = new TPolygonTool(fwsjcjMap, configPolygon);
        //添加标注事件
        TEvent.addListener(fwsjcjMarkPolygonTool, "draw", fwsjcjMarkDrawPolygon);
    }

//标注事件
    function fwsjcjMarkDrawPolygon(bounds, line) {
        fwsjcjMarkPolygonTool.close();
        if (fwsjcjMarkPolygonPoints.length >= 2) {
            //文字标注
            var text = $("#fwsjcj-MarkPopupText").val();//标注文字
            var markLng = fwsjcjMarkPolygonPoints[0];
            var markLat = fwsjcjMarkPolygonPoints[1];
            var configText = {
                text: text,
                offset: new TPixel(0, 0),
                position: new TLngLat(markLng, markLat)
            };
            //创建地图文本对象
            var label = new TLabel(configText);
            fwsjcjMap.addOverLay(label);//添加标注文字到地图上
            fwsjcjMarkLabels.push(label);//添加到标注文字数组
        }
        TEvent.removeListener(fwsjcjMarkMapClick);//关闭单击事件（保存坐标）
    }

//开始标注
    function fwsjcjCreateMarkPopup() {
        var str = "#fwsjcj";
        //设置标注工具栏的图标文字颜色为默认
        initSetMarkToolDivImg(str);
        //开始标注——设置图标样式
        initCreateMarkPopup(str);
        if ($(str + "-MarkPopupText").val() == "") {
            initAlert(str, "请先输入标注内容！", false);
        } else {
            //开启标注面绘制工具
            fwsjcjMarkPolygonTool.open();
            fwsjcjMarkPolygonPoints = [];
            fwsjcjMarkPolygonPoints.length = 0;  //清零保存的坐标

            if (fwsjcjMarkMapClick != "") {
                TEvent.removeListener(fwsjcjMarkMapClick); //避免加载多次点击事件
            }
            //标注地图点击事件
            fwsjcjMarkMapClick = TEvent.addListener(fwsjcjMap, "click", function (p) {
                fwsjcjMarkPolygonTool.open();
                var lnglat = fwsjcjMap.fromContainerPixelToLngLat(p);//获取点击处的坐标
                fwsjcjMarkPolygonPoints.push(lnglat.getLng());
                fwsjcjMarkPolygonPoints.push(lnglat.getLat());
                if (fwsjcjMarkPolygonPoints.length > 20) {
                    initAlert(str, "不能超过10个点！请重新选择范围！", false);
                    fwsjcjMarkPolygonTool.close();
                    fwsjcjMarkPolygonPoints.length = 0;
                    TEvent.removeListener(fwsjcjMarkMapClick);
                }
            });
        }
    }

//清空标注结果
    function fwsjcjMarkPopupClear() {
        var str = "#fwsjcj";
        //设置标注工具栏的图标文字颜色为默认
        initSetMarkToolDivImg(str);
        //清空标注结果——设置图标样式
        initMarkPopupClear(str);
        //清除所画的多边形
        fwsjcjMarkPolygonTool.clear();
        if (fwsjcjMarkLabels != null && fwsjcjMarkLabels.length > 0) {
            for (var i = 0; i < fwsjcjMarkLabels.length; i++) {
                fwsjcjMap.removeOverLay(fwsjcjMarkLabels[i]);
            }
        }
        fwsjcjMarkLabels = [];//清空标注文字
        $("#fwsjcj-MarkPopupText").val("");//清空标注文本框
    }

//取消标注
    function fwsjcjStopMarkPopup() {
        var str = "#fwsjcj";
        //设置标注工具栏的图标文字颜色为默认
        initSetMarkToolDivImg(str);
        //取消标注——设置图标样式
        initStopMarkPopup(str);
        //关闭标注面绘制工具
        fwsjcjMarkPolygonTool.close();
        if (fwsjcjMarkMapClick != "") {
            TEvent.removeListener(fwsjcjMarkMapClick);//避免加载多次点击事件
        }
    }

////////////////////////////////////////////////////////////////////////////////绘制工具
///////////////////////////////////////////////////////////////绘制点图标
//初始化绘制点图标工具
    function fwsjcjInitDrawPoint() {
        fwsjcjDrawPointMarkers = [];
        fwsjcjDrawMarkerTool = new TMarkTool(fwsjcjMap);
        //注册标注的mouseup事件
        TEvent.addListener(fwsjcjDrawMarkerTool, "mouseup", mouseup);

        //鼠标在地图上按下左键时添加一个点标记
        function mouseup(point) {
            var pointMarker = new TMarker(point);
            fwsjcjMap.addOverLay(pointMarker);
            fwsjcjDrawPointMarkers.push(pointMarker);
            //点击事件
            TEvent.addListener(pointMarker, "click", function () {
                if (fwsjcjSelectDeleteBool) {
                    fwsjcjMap.removeOverLay(pointMarker);
                }
            });
            //开启绘制点图标工具
            fwsjcjDrawMarkerTool.open();
        }
    }

//绘制点图标
    function fwsjcjDrawPoint() {
        var str = "#fwsjcj";
        //关闭绘制和删除选择功能
        fwsjcjStopDrawDeleteSelect();
        //绘制点图标——设置图标样式
        initDrawPoint(str);
        //开启绘制点图标工具
        fwsjcjDrawMarkerTool.open();
    }

///////////////////////////////////////////////////////////////绘制线
//初始化绘制线工具
    function fwsjcjInitDrawPolyline() {
        fwsjcjDrawLines = [];
        var config = {
            strokeColor: "red",	//折线颜色
            strokeWeight: "3px",	//折线的宽度，以像素为单位
            strokeOpacity: 0.5,	//折线的透明度，取值范围0 - 1
            strokeStyle: "solid",	//折线的样式，solid或dashed
            showLabel: false
        };
        //创建测距工具对象
        fwsjcjDrawLineTool = new TPolylineTool(fwsjcjMap, config);
        fwsjcjDrawLineTool.setTips("单击鼠标左键绘制折线点，双击完成绘制折线");
        //注册测距工具绘制完成后的事件
        TEvent.addListener(fwsjcjDrawLineTool, "draw", onDrawHZLine);

        //添加线
        function onDrawHZLine(bounds, line, obj) {
            fwsjcjDrawLineTool.clear();
            var polyline = new TPolyline(bounds, {
                strokeColor: "blue",
                strokeWeight: 3,
                strokeOpacity: 0.5,
                fillOpacity: 0.5
            });
            fwsjcjDrawLines.push(polyline);
            fwsjcjMap.addOverLay(polyline);//添加线
            //点击事件
            TEvent.addListener(polyline, "click", function () {
                if (fwsjcjSelectDeleteBool) {
                    fwsjcjMap.removeOverLay(polyline);
                }
            });
        }
    }

//绘制线
    function fwsjcjDrawLineString() {
        var str = "#fwsjcj";
        //关闭绘制和删除选择功能
        fwsjcjStopDrawDeleteSelect();
        //绘制线——设置图标样式
        initDrawLineString(str);
        //开启绘制线功能
        fwsjcjDrawLineTool.open();
    }

//清空任务图形
    function clearTaskPolygon() {
        $("#fwsjcj-contextMenuDiv").hide();
        $("#fwsjcjEx-contextMenuDiv").hide();
        if (taskPolygon != null) {
            fwsjcjMap.removeOverLay(taskPolygon);
        }
        if (polygonTaskTool != null) {
            polygonTaskTool.clear();
            polygonTaskTool.close();
        }
    }

    function taskLeftTitleSwitch(){
        clearTaskPolygon();
        if ($("#fwsjcj-taskDiv img").attr("title") == "结束新建") {
            $("#fwsjcj-taskDiv img").attr("title", "新建任务");
            $("#fwsjcj-taskDiv").children("span").html("新建任务");
        }
    }

    function isSegmentsIntersectant(segA, segB) {//线线
        const abc = (segA[0][0] - segB[0][0]) * (segA[1][1] - segB[0][1]) - (segA[0][1] - segB[0][1]) * (segA[1][0] - segB[0][0]);
        const abd = (segA[0][0] - segB[1][0]) * (segA[1][1] - segB[1][1]) - (segA[0][1] - segB[1][1]) * (segA[1][0] - segB[1][0]);
        if (abc * abd >= 0) {
            return false;
        }
        const cda = (segB[0][0] - segA[0][0]) * (segB[1][1] - segA[0][1]) - (segB[0][1] - segA[0][1]) * (segB[1][0] - segA[0][0]);
        const cdb = cda + abc - abd;
        return !(cda * cdb >= 0);
    }

    function isPolygonsIntersectant(plyA, plyB) {//面面
        for (let i = 0, il = plyA.length; i < il; i++) {
            for (let j = 0, jl = plyB.length; j < jl; j++) {
                const segA = [plyA[i], plyA[i === il - 1 ? 0 : i + 1]];
                const segB = [plyB[j], plyB[j === jl - 1 ? 0 : j + 1]];
                if (isSegmentsIntersectant(segA, segB)) {
                    return true;
                }
            }
        }
        return false;
    }


    // 2 判断包含
    //判断点是否在另一平面图中

    function pointInPolygon(point, vs) {
        const x = point[0], y = point[1];

        let inside = false;
        for (let i = 0, j = vs.length - 1; i < vs.length; j = i++) {
            const xi = vs[i][0], yi = vs[i][1];
            const xj = vs[j][0], yj = vs[j][1];

            const intersect = ((yi > y) !== (yj > y))
                && (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
            if (intersect) {
                inside = !inside;
            }
        }
        return inside;
    }

    //判断两多变形是否存在点与区域的包含关系(A的点在B的区域内或B的点在A的区域内)
    function isPointInPolygonBidirectional(plyA, plyB) {//面面
        let [a, b] = [false, false];
        a = plyA.some(item => pointInPolygon(item, plyB));
        if (!a) {
            b = plyB.some(item => pointInPolygon(item, plyA));
        }
        return a || b;
    }

    // 3 判断多边形是否重合
    function isPolygonsOverlap(plyA, plyB) {
        return isPolygonsIntersectant(plyA, plyB) || isPointInPolygonBidirectional(plyA, plyB);
    }

//任务接口

    var isAddTask=false;
    var mapmousemoveclick;
    var mapclick;

    function createTaskRegion() {
        clearTaskPolygon();

        if ($("#fwsjcj-taskDiv img").attr("title") == "新建任务") {
            $("#fwsjcj-taskDiv img").attr("title", "结束新建");
            $("#fwsjcj-taskDiv").children("span").html("结束新建");
            isAddTask = true;

            var index = Math.floor(Math.random() * 10);
            var color = colors[index];
            var config = {
                strokeColor: color, //折线颜色
                fillColor: "#FFFFFF",    //填充颜色。当参数为空时，折线覆盖物将没有填充效果
                strokeWeight: "3px", //折线的宽度，以像素为单位
                strokeOpacity: 1,  //折线的透明度，取值范围0 - 1
                fillOpacity: 0.2,        //填充的透明度，取值范围0 - 1
                showLabel: false             //是否显示页面，默认为true.
            };
            //创建任务工具对象
            polygonTaskTool = new TPolygonTool(fwsjcjMap, config);
            polygonTaskTool.setTips("单击鼠标左键绘制任务区域点，双击完成绘制任务区域");
            //注册测面工具绘制完成后的事件
            TEvent.addListener(polygonTaskTool, "draw", onDrawHZPolygon);
            polygonTaskTool.open();

            var selectPoint=[];
            if(mapclick!=""){
                TEvent.removeListener(mapclick); //避免加载多次点击事件
            }
            mapclick = TEvent.addListener(fwsjcjMap, "click", function (p) {
                var lnglat = fwsjcjMap.fromContainerPixelToLngLat(p);
                var hitPoint = [];
                hitPoint.push(lnglat.getLng());
                hitPoint.push(lnglat.getLat());
                selectPoint=hitPoint;
                if (fwsjcjTasks != null && fwsjcjTasks.length > 0) {
                    for (var i = 0; i < fwsjcjTasks.length; i++) {
                        var task = fwsjcjTasks[i];
                        if (task.mapPolygon == null) {
                            continue;
                        }
                        if (task.taskNum == "chinaregions") {
                            continue;
                        }
                        var points2 = lnglatdataswitch(task.mapPolygon.getLngLats());

                        if (pointInPolygon(hitPoint, points2) == true) {
                            Util.alert("该区域已被创建任务，请重新创建任务。");
                            taskLeftTitleSwitch();
                            if(mapclick!=""){
                                TEvent.removeListener(mapclick); //避免加载多次点击事件
                            }
                            if(mapmousemoveclick!=""){
                                TEvent.removeListener(mapmousemoveclick); //避免加载多次点击事件
                            }
                            return;
                        } else {
                        }
                    }
                }
                if(localProvinceRounds!=null){
                    var regionLngLats=[];
                    localProvinceRounds=localProvinceRounds.toString();
                    var proviceDatas=localProvinceRounds.split(",");
                    for(var i=0;i<proviceDatas.length;i++){
                        var datas=proviceDatas[i].split(" ");
                        var lnglat = new TLngLat(datas[0], datas[1]);
                        regionLngLats.push(lnglat);
                    }
                    var points = lnglatdataswitch(regionLngLats);
                    if (pointInPolygon(hitPoint, points) == false) {
                        Util.alert("超出用户行政区域，请重新创建任务。");
                        taskLeftTitleSwitch();
                        if(mapclick!=""){
                            TEvent.removeListener(mapclick); //避免加载多次点击事件
                        }
                        if(mapmousemoveclick!=""){
                            TEvent.removeListener(mapmousemoveclick); //避免加载多次点击事件
                        }
                        return;
                    } else {
                    }
                }
            });

            if(mapmousemoveclick!=""){
                TEvent.removeListener(mapmousemoveclick); //避免加载多次点击事件
            }

            mapmousemoveclick = TEvent.addListener(fwsjcjMap, "mousemove", function (p) {
                //将像素坐标转换成经纬度坐标
                if (isAddTask&&selectPoint.length>0) {
                    var lnglat = fwsjcjMap.fromContainerPixelToLngLat(p);
                    var hitPoint = [];
                    hitPoint.push(lnglat.getLng());
                    hitPoint.push(lnglat.getLat());
                    if (fwsjcjTasks != null && fwsjcjTasks.length > 0) {
                        for (var i = 0; i < fwsjcjTasks.length; i++) {
                            var task = fwsjcjTasks[i];
                            if (task.mapPolygon == null) {
                                continue;
                            }
                            if (task.taskNum == "chinaregions") {
                                continue;
                            }
                            var points2 = lnglatdataswitch(task.mapPolygon.getLngLats());

                            if (pointInPolygon(hitPoint, points2) == true) {
                                Util.alert("无法选择该区域。");
                                return;
                            } else {
                            }
                        }
                    }
                    if(localProvinceRounds!=null){
                        var regionLngLats=[];
                        localProvinceRounds=localProvinceRounds.toString();
                        var proviceDatas=localProvinceRounds.split(",");
                        for(var i=0;i<proviceDatas.length;i++){
                            var datas=proviceDatas[i].split(" ");
                            var lnglat = new TLngLat(datas[0], datas[1]);
                            regionLngLats.push(lnglat);
                        }
                        var points = lnglatdataswitch(regionLngLats);
                        if (pointInPolygon(hitPoint, points) == false) {
                            Util.alert("无法选择该区域。");
                            return;
                        } else {
                        }
                    }
                }
            });

            //添加多边形
            function onDrawHZPolygon(bounds, line) {
                PolygonPoints.length = 0;
                polygonTaskTool.clear();
                taskPolygon = new TPolygon(bounds, {
                    strokeColor: "blue",
                    strokeWeight: 3,
                    strokeOpacity: 1,
                    fillOpacity: 0.2
                });
                fwsjcjMap.addOverLay(taskPolygon);//添加多边形
                var areas = polygonTaskTool.getArea(bounds);
                taskAreas = areas / 1000000;
                //点击事件


                if (fwsjcjTasks != null && fwsjcjTasks.length > 0) {
                    var points1 = lnglatdataswitch(taskPolygon.getLngLats());
                    for (var i = 0; i < fwsjcjTasks.length; i++) {
                        var task = fwsjcjTasks[i];
                        if (task.mapPolygon == null) {
                            continue;
                        }
                        if (task.taskNum == "chinaregions") {
                            continue;
                        }
                        var points2 = lnglatdataswitch(task.mapPolygon.getLngLats());

                        if (isPolygonsOverlap(points1, points2) == true) {
                            Util.alert("任务区域不能重叠。");
                            taskLeftTitleSwitch();
                            return;
                        } else {
                        }
                    }
                }

                var points = taskPolygon.getLngLats();

                if (points != null && points.length > 0) {
                    var result = "";
                    for (var i = 0; i < points.length; i++) {
                        result += points[i].getLng() + "," + points[i].getLat() + ";";
                    }
                    result = result.substring(0, result.length - 1);
                }
                PolygonPoints = getTaskPointFormPoints(result);
                isAddTask = false;
                polygonTaskTool.close();
                if(mapclick!=""){
                    TEvent.removeListener(mapclick); //避免加载多次点击事件
                }
                if(mapmousemoveclick!="") {
                    TEvent.removeListener(mapmousemoveclick);
                }
            }
        } else {
            $("#fwsjcj-taskDiv img").attr("title", "新建任务");
            $("#fwsjcj-taskDiv").children("span").html("新建任务");
            isAddTask = false;
        }
    }

    function drawPolygonByPoint() {　//根据点坐标来画多边形
        //fwsjcjMap.removeOverLay(polygon);
        //alert(fwsjcjTasks.length);
        for (var j = 0; j < fwsjcjTasks.length; j++) {
            var task = fwsjcjTasks[j];
            var points = [];
            if (task.points.length != 0) {
                for (var i = 0; i < task.points.length; i = i + 2) {
                    points.push(new TLngLat(task.points[i], task.points[i + 1]));
                }

                //创建面对象
                var color = "darkgreen";
                if (task.state == 1) {
                    color = "orange";
                } else if (task.state == 0) {
                    color = "red";
                }
                var polygon = new TPolygon(points, {
                    strokeColor: color,
                    strokeWeight: "2px",
                    //strokeWeight: 2,
                    strokeOpacity: 1,
                    fillOpacity: 0.2
                });
                //向地图上添加面
                fwsjcjMap.addOverLay(polygon);
                task.mapPolygon = polygon;
                //var polygon1Points = pathConvert(polygon);
                //debugger;
                var maxLng=task.points[0];
                var maxIndex=0;
                for(var i=2;i<task.points.length;i+=2){
                    if(maxLng<task.points[i]){
                        maxIndex=i;
                        maxLng=task.points[i];
                    }
                }
                var markLng = task.points[maxIndex];
                var markLat = task.points[maxIndex+1];
                //debugger;
                var configText = {
                    text: task.name,
                    offset: new TPixel(0, 0),
                    position: new TLngLat(markLng, markLat)
                };
                //创建地图文本对象
                var label = new TLabel(configText);
                fwsjcjMap.addOverLay(label);//添加标注文字到地图上
                fwsjcjMarkLabels.push(label);//添加到标注文字数组

            } else {
                //initAlert("#fwsjcj","没有选择区域",false);
            }

        }
    }

//显示右键菜单
 /*   function initShowContextMenu(str, type, x, y) {
        //设置右键菜单位置
        var left = x;
        //获取右键菜单位置left最大值
        var maxLeft = $("body").width() - ($(str + "-leftContextMenuDiv").width() + 1);
        if (left > maxLeft) {
            left = maxLeft;
        }

        var top = y - $(str + "-mapDiv").offset().top;
        //获取右键菜单位置top最大值
        var maxTop = $("body").height() - ($(str + "-leftContextMenuDiv").height() + 1);
        if (top > maxTop) {
            top = maxTop;
        }
        if (type == 1) {
            $(str + "-contextMenuDiv").css("left", left + "px");
            $(str + "-contextMenuDiv").css("top", top+40 + "px");
            $(str + "-contextMenuDiv").show();
        } else if (type == 2) {
            $(str + "Ex-contextMenuDiv").css("left", left + "px");
            $(str + "Ex-contextMenuDiv").css("top", top+40 + "px");
            $(str + "Ex-contextMenuDiv").show();
        } else if (type == 3) {
            $(str + "FW-contextMenuDiv").css("left", left + "px");
            $(str + "FW-contextMenuDiv").css("top", top+80 + "px");
            $(str + "FW-contextMenuDiv").show();
        }
    }*/

//////////////////////////////////////////////////////////////////////判断点是否在面内
//判断点是否在面内
    /*function initPointInsidePolygon(pointsArray, LonLat) {
        var bool = false;//点不在面内
        if (LonLat != null) {
            var x = LonLat[0];
            var y = LonLat[1];
            var length = pointsArray.length;
            for (var i = 0, j = length - 1; i < length; j = i++) {
                var xi = pointsArray[i][0];
                var yi = pointsArray[i][1];
                var xj = pointsArray[j][0];
                var yj = pointsArray[j][1];
                var intersect = ((yi > y) != (yj > y))
                    && (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
                if (intersect) {
                    bool = !bool;
                }
            }
        }
        return bool;
    }*/

///////////////////////////////////////////////////////////////绘制多边形
//初始化绘制多边形工具
    function fwsjcjInitDrawPolygon() {
        fwsjcjDrawPolygons = [];
        var config = {
            strokeColor: "red",	//折线颜色
            fillColor: "#FFFFFF",	//填充颜色。当参数为空时，折线覆盖物将没有填充效果
            strokeWeight: "3px",	//折线的宽度，以像素为单位
            strokeOpacity: 0.5,	//折线的透明度，取值范围0 - 1
            fillOpacity: 0.5,			//填充的透明度，取值范围0 - 1
            showLabel: false
        };
        //创建测面工具对象
        fwsjcjDrawPolygonTool = new TPolygonTool(fwsjcjMap, config);
        fwsjcjDrawPolygonTool.setTips("单击鼠标左键绘制多边形点，双击完成绘制多边形");
        //注册测面工具绘制完成后的事件
        TEvent.addListener(fwsjcjDrawPolygonTool, "draw", onDrawHZPolygon);

        //添加多边形
        function onDrawHZPolygon(bounds, line) {
            fwsjcjDrawPolygonTool.clear();
            var polygon = new TPolygon(bounds, {
                strokeColor: "blue",
                strokeWeight: 3,
                strokeOpacity: 0.5,
                fillOpacity: 0.5
            });
            fwsjcjDrawPolygons.push(polygon);
            fwsjcjMap.addOverLay(polygon);//添加多边形
            //点击事件
            TEvent.addListener(polygon, "click", function () {
                if (fwsjcjSelectDeleteBool) {
                    fwsjcjMap.removeOverLay(polygon);
                }
            });
        }
    }

//绘制多边形
    function fwsjcjDrawPolygon() {
        var str = "#fwsjcj";
        //关闭绘制和删除选择功能
        fwsjcjStopDrawDeleteSelect();
        //绘制多边形——设置图标样式
        initDrawPolygon(str);
        //开启绘制多边形功能
        fwsjcjDrawPolygonTool.open();
    }

///////////////////////////////////////////////////////////////绘制长方形
//初始化绘制长方形工具
    function fwsjcjInitDrawRect() {
        fwsjcjDrawRects = [];
        var config = {
            strokeColor: "#000000",	//折线颜色
            fillColor: "#FFFFFF",	//填充颜色。当参数为空时，折线覆盖物将没有填充效果
            strokeWeight: "2px",	//折线的宽度，以像素为单位
            opacity: 0.5,	//折线的透明度，取值范围0 - 1
            strokeStyle: "solid"	//折线的样式，solid或dashed
        };
        //创建矩形工具对象
        fwsjcjDrawRectTool = new TRectTool(fwsjcjMap, config);
        //注册矩形工具绘制完成后的事件
        TEvent.addListener(fwsjcjDrawRectTool, "draw", onDrawHZRect);

        //添加长方形
        function onDrawHZRect(bounds) {
            fwsjcjDrawRectTool.clear();
            var rect = new TRect(bounds);

            fwsjcjDrawRects.push(rect);
            fwsjcjMap.addOverLay(rect);//添加长方形
            //点击事件
            TEvent.addListener(rect, "click", function () {
                if (fwsjcjSelectDeleteBool) {
                    fwsjcjMap.removeOverLay(rect);
                }
            });
        }
    }

//绘制长方形
    function fwsjcjShapeDrawBox() {
        var str = "#fwsjcj";
        //关闭绘制和删除选择功能
        fwsjcjStopDrawDeleteSelect();
        //绘制长方形——设置图标样式
        initShapeDrawBox(str);
        //开启绘制长方形功能
        fwsjcjDrawRectTool.open();
    }

///////////////////////////////////////////////////////////////绘制圆
//初始化绘制圆工具
    function fwsjcjInitDrawCircle() {
        fwsjcjDrawCircles = [];
        var config = {
            strokeColor: "#000000",	//圆边线颜色
            fillColor: "#FFFFFF",	//填充颜色。
            strokeWeight: "3px",	//圆边线线的宽度，以像素为单位
            strokeOpacity: 0.5,	//圆边线线的透明度，取值范围0 - 1
            fillOpacity: 0.5,			//填充的透明度，取值范围0 - 1
            strokeStyle: "solid"	//圆边线线的样式，solid或dashed
        };
        //注册画圆工具绘制完成后的事件
        TEvent.addListener(fwsjcjDrawCircleTool, 'drawend', onDrawCircleEnd);

        //添加圆
        function onDrawCircleEnd(circle) {
            fwsjcjDrawCircleTool.clear();
            var center = circle.getCenter();
            var radius = circle.getRadius();
            var circle = new TCircle(center, radius);
            fwsjcjDrawCircles.push(circle);
            fwsjcjMap.addOverLay(circle);//添加圆
            //点击事件
            TEvent.addListener(circle, "click", function () {
                if (fwsjcjSelectDeleteBool) {
                    fwsjcjMap.removeOverLay(circle);
                }
            });
        }
    }

//绘制圆
    function fwsjcjDrawCircle() {
        var str = "#fwsjcj";
        //关闭绘制和删除选择功能
        fwsjcjStopDrawDeleteSelect();
        //绘制圆——设置图标样式
        initDrawCircle(str);
        //开启绘制圆功能
        fwsjcjDrawCircleTool.open();
    }

///////////////////////////////////////////////////////////////绘制处理功能
//结束绘制
    function fwsjcjStopDrawTool() {
        var str = "#fwsjcj";
        //结束绘制——设置图标样式
        initStopDrawTool(str);
        //关闭所有绘制工具
        fwsjcjCloseAllDrawTools();
    }

//关闭所有绘制工具
    function fwsjcjCloseAllDrawTools() {
        //关闭绘制功能
        fwsjcjDrawMarkerTool.close();//关闭绘制点图标工具
        fwsjcjDrawLineTool.close();//关闭绘制线工具
        fwsjcjDrawPolygonTool.close();//关闭绘制多边形工具
        fwsjcjDrawRectTool.close();//关闭绘制长方形工具
        fwsjcjDrawCircleTool.close();//关闭绘制圆工具
    }

//清除绘制结果
    function fwsjcjDrawClear() {
        var str = "#fwsjcj";
        //关闭绘制和删除选择功能
        fwsjcjStopDrawDeleteSelect();
        //清除绘制结果——设置图标样式
        initDrawClear(str);
        //清空绘制结果
        //清空点图标
        if (fwsjcjDrawPointMarkers != null && fwsjcjDrawPointMarkers.length > 0) {
            for (var i = 0; i < fwsjcjDrawPointMarkers.length; i++) {
                fwsjcjMap.removeOverLay(fwsjcjDrawPointMarkers[i]);
            }
        }
        //清空线
        if (fwsjcjDrawLines != null && fwsjcjDrawLines.length > 0) {
            for (var i = 0; i < fwsjcjDrawLines.length; i++) {
                fwsjcjMap.removeOverLay(fwsjcjDrawLines[i]);
            }
        }
        //清空多边形
        if (fwsjcjDrawPolygons != null && fwsjcjDrawPolygons.length > 0) {
            for (var i = 0; i < fwsjcjDrawPolygons.length; i++) {
                fwsjcjMap.removeOverLay(fwsjcjDrawPolygons[i]);
            }
        }
        //清空长方形
        if (fwsjcjDrawRects != null && fwsjcjDrawRects.length > 0) {
            for (var i = 0; i < fwsjcjDrawRects.length; i++) {
                fwsjcjMap.removeOverLay(fwsjcjDrawRects[i]);
            }
        }
        //清空圆
        if (fwsjcjDrawCircles != null && fwsjcjDrawCircles.length > 0) {
            for (var i = 0; i < fwsjcjDrawCircles.length; i++) {
                fwsjcjMap.removeOverLay(fwsjcjDrawCircles[i]);
            }
        }
    }

///////////////////////////////////////////////////////////////选择功能
//初始化选择工具
    function fwsjcjInitSelectTool() {
        fwsjcjSelectDeleteBool = false;//是否删除选择的已绘制图形的参数
    }

//删除选择功能
    function fwsjcjDrawSelectDelete() {
        var str = "#fwsjcj";
        //关闭所有绘制工具
        fwsjcjCloseAllDrawTools();
        //设置绘制工具栏的图标文字颜色为默认
        initSetDrawToolDivImg(str);
        //删除选择功能
        var html = $(str + "-SelectDrawBtn span").html();
        if (html == "开启删除") {//开启删除选择功能
            //开启删除选择工具
            fwsjcjRemoveSelectDrawTool(str, true);

        } else {//关闭删除选择功能
            //关闭删除选择工具
            fwsjcjRemoveSelectDrawTool(str, false);
        }
    }

//关闭删除选择工具
    function fwsjcjRemoveSelectDrawTool(str, bool) {
        //设置是否删除选择的已绘制图形的参数
        fwsjcjSelectDeleteBool = bool;
        //关闭删除选择工具——设置图标样式
        initRemoveSelectDrawTool(str, bool);
    }

//关闭绘制和删除选择功能
    function fwsjcjStopDrawDeleteSelect() {
        var str = "#fwsjcj";
        //关闭所有绘制工具
        fwsjcjCloseAllDrawTools();
        //关闭删除选择工具
        fwsjcjRemoveSelectDrawTool(str, false);
        //设置绘制工具栏的图标文字颜色为默认
        initSetDrawToolDivImg(str);
    }

////////////////////////////////////////////////////////////////////////////////测量工具
//初始化测量工具
    function fwsjcjInitMeasureTool() {
        //测距线样式
        var configLine = {
            strokeColor: "blue",	//折线颜色
            strokeWeight: "3px",	//折线的宽度，以像素为单位
            strokeOpacity: 0.5,	//折线的透明度，取值范围0 - 1
            strokeStyle: "solid"	//折线的样式，solid或dashed
        };
        //创建测距工具对象
        fwsjcjMeasureLineTool = new TPolylineTool(fwsjcjMap, configLine);
        TEvent.addListener(fwsjcjMeasureLineTool, "draw", onDrawCJLine);//测距完成事件
        //关闭测距工具
        function onDrawCJLine(bounds, line, obj) {
            //开启测距工具
            fwsjcjMeasureLineTool.open();
        }

        //测面样式
        var configPolygon = {
            strokeColor: "blue",	//折线颜色
            fillColor: "#FFFFFF",	//填充颜色。当参数为空时，折线覆盖物将没有填充效果
            strokeWeight: "3px",	//折线的宽度，以像素为单位
            strokeOpacity: 0.5,	//折线的透明度，取值范围0 - 1
            fillOpacity: 0.5			//填充的透明度，取值范围0 - 1
            //showLabel:false
        };
        //创建测面工具对象
        fwsjcjMeasurePolygonTool = new TPolygonTool(fwsjcjMap, configPolygon);
        TEvent.addListener(fwsjcjMeasurePolygonTool, "draw", onDrawCMPolygon);//测面完成事件
        //关闭测面工具
        function onDrawCMPolygon(bounds, line) {
            //开启测面工具
            fwsjcjMeasurePolygonTool.open();
            //var areas=fwsjcjMeasurePolygonTool.getArea(bounds);
            //debugger;
        }
    }

//测量距离
    function fwsjcjMeasureLength() {
        var str = "#fwsjcj";
        //设置测量工具栏的图标文字颜色为默认
        initSetMeasureToolDivImg(str);
        //测量距离——设置图标样式
        initMeasureLength(str);
        //关闭测面工具
        fwsjcjMeasurePolygonTool.close();
        //开启测距工具
        fwsjcjMeasureLineTool.open();
    }

//测量面积
    function fwsjcjMeasureArea() {
        var str = "#fwsjcj";
        //设置测量工具栏的图标文字颜色为默认
        initSetMeasureToolDivImg(str);
        //测量面积——设置图标样式
        initMeasureArea(str);
        //关闭测距工具
        fwsjcjMeasureLineTool.close();
        //开启测面工具
        fwsjcjMeasurePolygonTool.open();
    }

//关闭测量功能
    function fwsjcjStopMeasureTool() {
        var str = "#fwsjcj";
        //关闭测量功能——设置图标样式
        initStopMeasureTool(str);
        //关闭测量工具
        fwsjcjMeasureLineTool.close();//关闭测距工具
        fwsjcjMeasurePolygonTool.close();//关闭测面工具
    }

//清空测量结果
    function fwsjcjMeasureClear() {
        var str = "#fwsjcj";
        //设置测量工具栏的图标文字颜色为默认
        initSetMeasureToolDivImg(str);
        //清空测量结果——设置图标样式
        initMeasureClear(str);
        //关闭测量工具
        fwsjcjMeasureLineTool.close();//关闭测距工具
        fwsjcjMeasurePolygonTool.close();//关闭测面工具
        //清空测距工具
        fwsjcjMeasureLineTool.clear();
        //清空测面工具
        fwsjcjMeasurePolygonTool.clear();
    }

////////////////////////////////////////////////////////////////////////////////定位工具
//获取鼠标经纬度
    function fwsjcjMousePositionCenter() {
        var str = "#fwsjcj";
        //获取鼠标经纬度——设置图标样式
        initMousePositionCenter(str);
        //鼠标经纬度
        var lng = $(str + "-MousePositionLon").val();
        var lat = $(str + "-MousePositionLat").val();
        if (lng != "" && lat != "") {
            //地图居中到定位图标位置
            fwsjcjMap.setCenter(new TLngLat(lng, lat));
            //定位图标
            var icon = new TIcon("/static/app/fwsjcj/taskmanage/image/marker2.png", new TSize(19, 27), {anchor: new TPixel(9, 27)});
            var marker = new TMarker(new TLngLat(lng, lat), {icon: icon});
            //图标文字
            var title = "经度：" + lng + "，纬度：" + lat;
            marker.setTitle(title);
            //添加定位图标到地图叠加层
            fwsjcjMap.addOverLay(marker);
            //添加到定位图标数组中
            fwsjcjPositionMarkers.push(marker);
            //注册标注点的点击事件
            TEvent.bind(marker, "click", marker, function () {
                var lnglat = this.getLngLat();
                //获取经纬度
                var fmapLonLat = new FMapLonLat({lnglat: lnglat});
                var lonlat = fmapLonLat.getLonLatByLngLat();
                //根据经纬度获取省市边界线
                fwsjcjLonLatGetRegion(lonlat);
                //地图居中缩放
                fwsjcjSetCenterZoom(lonlat, 18);
            });

        } else {
        }
    }

//清空定位结果
    function fwsjcjMousePositionClear() {
        var str = "#fwsjcj";
        //清空定位结果——设置图标样式
        initMousePositionClear(str);
        //清空定位图标图层
        if (fwsjcjPositionMarkers != null && fwsjcjPositionMarkers.length > 0) {
            for (var i = 0; i < fwsjcjPositionMarkers.length; i++) {
                fwsjcjMap.removeOverLay(fwsjcjPositionMarkers[i]);
            }
        }
        fwsjcjPositionMarkers = [];
        //清空经纬度文本框
        $(str + "-MousePositionLon").val("");
        $(str + "-MousePositionLat").val("");
    }

//开始标注——设置图标样式
    function initCreateMarkPopup(str) {
        //设置图标颜色
        $(str + "-MarkPopupBtn span").css("color", "#00a7ff");
        $(str + "-MarkPopupBtn img").attr("src", "/static/app/fwsjcj/taskmanage/image/biaoz-r.png");
    }

//清空标注结果——设置图标样式
    function initMarkPopupClear(str) {
        //设置图标颜色
        $(str + "-bz_qkjg span").css("color", "#00a7ff");
        $(str + "-bz_qkjg img").attr("src", "/static/app/fwsjcj/taskmanage/image/qux-r.png");
    }

//取消标注——设置图标样式
    function initStopMarkPopup(str) {
        //设置图标颜色
        $(str + "-bz_qxbz span").css("color", "#00a7ff");
        $(str + "-bz_qxbz img").attr("src", "/static/app/fwsjcj/taskmanage/image/qxbiaoz-r.png");
    }

//绘制点图标——设置图标样式
    function initDrawPoint(str) {
        //设置图标颜色
        $(str + "-hz_dian span").css("color", "#00a7ff");
        $(str + "-hz_dian img").attr("src", "/static/app/fwsjcj/taskmanage/image/dian-r.png");
    }

//绘制线——设置图标样式
    function initDrawLineString(str) {
        //设置图标颜色
        $(str + "-hz_zx span").css("color", "#00a7ff");
        $(str + "-hz_zx img").attr("src", "/static/app/fwsjcj/taskmanage/image/xian-rr.png");
    }

//绘制多边形——设置图标样式
    function initDrawPolygon(str) {
        //设置图标颜色
        $(str + "-hz_dbx span").css("color", "#00a7ff");
        $(str + "-hz_dbx img").attr("src", "/static/app/fwsjcj/taskmanage/image/dbx-r.png");
    }

//绘制长方形——设置图标样式
    function initShapeDrawBox(str) {
        //设置图标颜色
        $(str + "-hz_cfx span").css("color", "#00a7ff");
        $(str + "-hz_cfx img").attr("src", "/static/app/fwsjcj/taskmanage/image/cfx-r.png");
    }

//绘制圆——设置图标样式
    function initDrawCircle(str) {
        //设置图标颜色
        $(str + "-hz_yuan span").css("color", "#00a7ff");
        $(str + "-hz_yuan img").attr("src", "/static/app/fwsjcj/taskmanage/image/yuan-r.png");
    }

//结束绘制——设置图标样式
    function initStopDrawTool(str) {
        //设置绘制工具栏的图标文字颜色为默认
        $(str + "-hz_dian span").css("color", "#fff");
        $(str + "-hz_dian img").attr("src", "/static/app/fwsjcj/taskmanage/image/dian.png");//点
        $(str + "-hz_zx span").css("color", "#fff");
        $(str + "-hz_zx img").attr("src", "/static/app/fwsjcj/taskmanage/image/xian.png");//折线
        $(str + "-hz_dbx span").css("color", "#fff");
        $(str + "-hz_dbx img").attr("src", "/static/app/fwsjcj/taskmanage/image/dbx.png");//多边形
        $(str + "-hz_cfx span").css("color", "#fff");
        $(str + "-hz_cfx img").attr("src", "/static/app/fwsjcj/taskmanage/image/cfx.png");//长方形
        $(str + "-hz_yuan span").css("color", "#fff");
        $(str + "-hz_yuan img").attr("src", "/static/app/fwsjcj/taskmanage/image/yuan.png");//圆
        $(str + "-hz_qk span").css("color", "#fff");
        $(str + "-hz_qk img").attr("src", "/static/app/fwsjcj/taskmanage/image/qux.png");//清空结果
        //设置结束绘制图标颜色
        $(str + "-hz_jshz span").css("color", "#00a7ff");
        $(str + "-hz_jshz img").attr("src", "/static/app/fwsjcj/taskmanage/image/jshz-r.png");
    }

//清除绘制结果——设置图标样式
    function initDrawClear(str) {
        //设置图标颜色
        $(str + "-hz_qk span").css("color", "#00a7ff");
        $(str + "-hz_qk img").attr("src", "/static/app/fwsjcj/taskmanage/image/qux-r.png");
    }

//关闭删除选择工具——设置图标样式
    function initRemoveSelectDrawTool(str, bool) {
        //设置图标
        if (bool) {
            $(str + "-SelectDrawBtn span").html("关闭删除");
            $(str + "-SelectDrawBtn span").css("color", "#00a7ff");
            $(str + "-SelectDrawBtn img").attr("src", "/static/app/fwsjcj/taskmanage/image/kqxz-r.png");
        } else {
            $(str + "-SelectDrawBtn span").html("开启删除");
            $(str + "-SelectDrawBtn span").css("color", "#fff");
            $(str + "-SelectDrawBtn img").attr("src", "/static/app/fwsjcj/taskmanage/image/kqxz.png");
        }
    }

//测量距离——设置图标样式
    function initMeasureLength(str) {
        //设置图标颜色
        $(str + "-cl_jl span").css("color", "#00a7ff");
        $(str + "-cl_jl img").attr("src", "/static/app/fwsjcj/taskmanage/image/jul-r.png");
    }

//测量面积——设置图标样式
    function initMeasureArea(str) {
        //设置图标颜色
        $(str + "-cl_mj span").css("color", "#00a7ff");
        $(str + "-cl_mj img").attr("src", "/static/app/fwsjcj/taskmanage/image/mianj-r.png");
    }

//关闭测量功能——设置图标样式
    function initStopMeasureTool(str) {
        //设置绘制工具栏的图标文字颜色为默认
        $(str + "-cl_jl span").css("color", "#fff");
        $(str + "-cl_jl img").attr("src", "/static/app/fwsjcj/taskmanage/image/jul.png");//测距
        $(str + "-cl_mj span").css("color", "#fff");
        $(str + "-cl_mj img").attr("src", "/static/app/fwsjcj/taskmanage/image/mianj.png");//测面
        $(str + "-cl_qk span").css("color", "#fff");
        $(str + "-cl_qk img").attr("src", "/static/app/fwsjcj/taskmanage/image/qux.png");//清空结果
        //设置结束测量图标颜色
        $(str + "-cl_jscl span").css("color", "#00a7ff");
        $(str + "-cl_jscl img").attr("src", "/static/app/fwsjcj/taskmanage/image/jshz-r.png");
    }

//清空测量结果——设置图标样式
    function initMeasureClear(str) {
        //设置图标颜色
        $(str + "-cl_qk span").css("color", "#00a7ff");
        $(str + "-cl_qk img").attr("src", "/static/app/fwsjcj/taskmanage/image/qux-r.png");
    }

//获取鼠标经纬度——设置图标样式
    function initMousePositionCenter(str) {
        $(str + "-MousePositionDiv span").css("color", "#fff");
        $(str + "-dw_tj span").css("color", "#00a7ff");
        $(str + "-dw_tj img").attr("src", "/static/app/fwsjcj/taskmanage/image/dingwd-r.png");
        $(str + "-dw_qk img").attr("src", "/static/app/fwsjcj/taskmanage/image/qux.png");
    }

//清空定位结果——设置图标样式
    function initMousePositionClear(str) {
        $(str + "-MousePositionDiv span").css("color", "#fff");
        $(str + "-dw_qk span").css("color", "#00a7ff");
        $(str + "-dw_tj img").attr("src", "/static/app/fwsjcj/taskmanage/image/dingwd.png");
        $(str + "-dw_qk img").attr("src", "/static/app/fwsjcj/taskmanage/image/qux-r.png");
    }


    function fwsjcjSetToolsBoxDiv() {

    }

    function fwsjcjInitEvent() {
        //天地图事件
        $("#fwsjcj-ZoomDefaultPosition").unbind("click");
        $("#fwsjcj-ZoomDefaultPosition").click(function () {
            fwsjcjZoomDefaultPosition();
        });
        $("#fwsjcj-setToolsBoxDiv").unbind("click");
        $("#fwsjcj-setToolsBoxDiv").click(function () {
            initSetToolsBoxDiv('#fwsjcj');
        });
        $("#fwsjcj-taskDiv").unbind("click");
        $("#fwsjcj-taskDiv").click(function () {
            createTaskRegion();
        });
        $("#fwsjcj-fwDisplay").unbind("click");
        $("#fwsjcj-fwDisplay").click(function () {
            displayFWDatas();
        });
        $("#fwsjcj-biaozhu").unbind("click");
        $("#fwsjcj-biaozhu").click(function () {
            initMarkPopupTool('#fwsjcj');
        });
        $("#fwsjcj-celiang").unbind("click");
        $("#fwsjcj-celiang").click(function () {
            initMeasureTool('#fwsjcj');
        });
        $("#fwsjcj-dingwei").unbind("click");
        $("#fwsjcj-dingwei").click(function () {
            initMousePositionTool('#fwsjcj');
        });
        $("#fwsjcj-bz_qkjg").unbind("click");
        $("#fwsjcj-bz_qkjg").click(function () {
            fwsjcjStopMarkPopup();
        });
        $("#fwsjcj-MarkPopupBtn").unbind("click");
        $("#fwsjcj-MarkPopupBtn").click(function () {
            fwsjcjCreateMarkPopup();
        });
        $("#fwsjcj-cl_jl").unbind("click");
        $("#fwsjcj-cl_jl").click(function () {
            fwsjcjMeasureLength();
        });
        $("#fwsjcj-cl_mj").unbind("click");
        $("#fwsjcj-cl_mj").click(function () {
            fwsjcjMeasureArea();
        });
        $("#fwsjcj-cl_jscl").unbind("click");
        $("#fwsjcj-cl_jscl").click(function () {
            fwsjcjStopMeasureTool();
        });
        $("#fwsjcj-cl_qk").unbind("click");
        $("#fwsjcj-cl_qk").click(function () {
            fwsjcjMeasureClear();
        });
        $("#fwsjcj-dw_qk").unbind("click");
        $("#fwsjcj-dw_qk").click(function () {
            fwsjcjMousePositionClear();
        });
        $("#fwsjcj-dw_tj").unbind("click");
        $("#fwsjcj-dw_tj").click(function () {
            fwsjcjMousePositionCenter();
        });
       ;
        fwsjcjTMapEvent();
    }

//天地图事件
    function fwsjcjTMapEvent() {
        //地图事件
        fwsjcjMap = initTMapEvent("#fwsjcj", fwsjcjMap);

        //地图鼠标点击事件
        TEvent.addListener(fwsjcjMap, "click", function (p, btn) {
            //将像素坐标转换成经纬度坐标
            var lnglat = fwsjcjMap.fromContainerPixelToLngLat(p);
            var Lon = lnglat.getLng();
            var Lat = lnglat.getLat();
            $("#fwsjcj-contextMenuDiv").hide();
            $("#fwsjcjEx-contextMenuDiv").hide();
            $("#fwsjcjFW-contextMenuDiv").hide();
            if (btn == 1) {//鼠标左键
                //获取鼠标位置的经纬度
                $("#fwsjcj-MousePositionLon").val(Lon);
                $("#fwsjcj-MousePositionLat").val(Lat);
            } else if (btn == 2) {
                var hitPoint = [];
                hitPoint[0] = Lon;
                hitPoint[1] = Lat;
                if (PolygonPoints != null && PolygonPoints.length > 2) {
                    var points = [];
                    for (var i = 0; i < PolygonPoints.length; i += 2) {
                        var pp = [];
                        pp[0] = PolygonPoints[i];
                        pp[1] = PolygonPoints[i + 1];
                        points.push(pp);
                    }
                    if (initPointInsidePolygon(points, hitPoint)) {
                        initShowContextMenu("#fwsjcj", 1, p[0], p[1]);
                        fwsjcjCreateContextMenu();
                        return;
                    }
                } else {
                }
                if (fwsjcjTasks != null && fwsjcjTasks.length > 0) {
                    for (var j = 0; j < fwsjcjTasks.length; j++) {
                        var task = fwsjcjTasks[j];
                        if(task.taskNum=="chinaregions"){
                            continue;
                        }
                        var points = [];
                        for (var i = 0; i < task.points.length; i += 2) {
                            var pp = [];
                            pp.push(task.points[i]);
                            pp.push(task.points[i + 1]);
                            points.push(pp);
                        }

                        if (initPointInsidePolygon(points, hitPoint)) {
                            initShowContextMenu("#fwsjcj", 2, p[0], p[1]);

                            fwsjcjOperateContextMenu(task);
                            return;
                        }
                    }
                }
            }
        });
    }

    function getTaskPointFormPoints(tPoints) {
        var points2 = [];
        var test = tPoints.split(/,|;/);
        for (var i = 0; i < test.length; i++) {
            var ff = parseFloat(test[i]);
            points2.push(ff);
        }
        return points2;
    }

    function fwsjcjOperateContextMenu(task) {
        //设置右键菜单点击事件
        $("#fwsjcj-modifyAreaContextMenuLi").off("click");
        $("#fwsjcj-modifyAreaContextMenuLi").on("click", function () {
            $("#fwsjcjEx-contextMenuDiv").hide();
            if(task.state == '2'){
                Util.alert("只有任务状态为未分配和进行中的才能修改区域!");
                return;
            }
            if (task.mapPolygon != null) {
                task.mapPolygon.enableEdit();
                task.mapPolygon.onChange(function(){
                    var points1=lnglatdataswitch(task.mapPolygon.getLngLats());
                    if (fwsjcjTasks != null && fwsjcjTasks.length > 0) {
                        for (var i = 0; i < fwsjcjTasks.length; i++) {
                            var task1 = fwsjcjTasks[i];
                            if(task1.taskNum=="chinaregions"){
                                continue;
                            }
                            var points2=lnglatdataswitch(task1.mapPolygon.getLngLats());
                            if (task.id!=task1.id&&isPolygonsOverlap(points1,points2) == true) {
                                Util.alert("无法选择该区域。");
                                return;
                            } else {
                            }
                        }
                    }
                });
            }
        });

        $("#fwsjcj-endDrawTaskContextMenuLi").off("click");
        $("#fwsjcj-endDrawTaskContextMenuLi").on("click", function () {
            if (task.points != null && task.points.length > 0) {
                task.mapPolygon.disableEdit();
                $("#fwsjcjEx-contextMenuDiv").hide();
                var points1=lnglatdataswitch(task.mapPolygon.getLngLats());
                if (fwsjcjTasks != null && fwsjcjTasks.length > 0) {
                    for (var i = 0; i < fwsjcjTasks.length; i++) {
                        var task1 = fwsjcjTasks[i];
                        if(task1.taskNum=="chinaregions"){
                            continue;
                        }
                        var points2=lnglatdataswitch(task1.mapPolygon.getLngLats());
                        if (task.id!=task1.id&&isPolygonsOverlap(points1,points2) == true) {
                            Util.alert("任务区域不能重叠。");
                            initOperateData();
                            return;
                        } else {
                        }
                    }
                }

                var points = task.mapPolygon.getLngLats();
                if (points != null && points.length > 0) {
                    var result = "";
                    for (var i = 0; i < points.length; i++) {
                        result += points[i].getLng() + "," + points[i].getLat() + ";";
                    }
                    result = result.substring(0, result.length - 1);
                }
                task.points = getTaskPointFormPoints(result);

                var maxLng=task.points[0];
                var maxIndex=0;
                for(var i=2;i<task.points.length;i+=2){
                    if(maxLng<task.points[i]){
                        maxIndex=i;
                        maxLng=task.points[i];
                    }
                }
                var markLng = task.points[maxIndex];
                var markLat = task.points[maxIndex+1];
                //fwsjcjMarkLabels.push(label);//添加到标注文字数组
                for(var i =0;i<fwsjcjMarkLabels.length;i++){
                    var label=fwsjcjMarkLabels[i];
                    if(label.getLabel()==task.name){
                        label.setLngLat( new TLngLat(markLng, markLat));
                        break;
                    }
                }

            }
        });

        $("#fwsjcj-modifyContextMenuLi").off("click");
        $("#fwsjcj-modifyContextMenuLi").on("click", function () {
            $("#fwsjcjEx-contextMenuDiv").hide();
            /*if(task.state != '0'){
                Util.alert("只有任务状态为未分配的才能修改任务!");
                return;
            }*/
            if(task.state == '2'){
                Util.alert("只有任务状态为未分配和进行中的才能修改任务!");
                return;
            }
            if (task.points != null && task.points.length > 0) {
                //$("#fwsjcjEx-contextMenuDiv").hide();
                var result = "";
                for (var i = 0; i < task.points.length; i += 2) {
                    result += task.points[i] + "," + task.points[i + 1] + ";";
                }
                var lnglats=[];
                for (var i = 0; i < task.points.length; i += 2) {
                    var point=new TLngLat(task.points[i], task.points[i + 1]);
                    lnglats.push(point);
                }
                var tpTool = new TPolygonTool(fwsjcjMap,null);
                tpTool.open();
                var areas=tpTool.getArea(lnglats);
                taskAreas = areas/1000000;
                //debugger;
                task.area=taskAreas;
                tpTool.close();
                tpTool=null;
                result = result.substr(0, result.length - 1);
                _viewTaskSet(1,result,task);
            }
        });

        $("#fwsjcj-deleteContextMenuLi").off("click");
        $("#fwsjcj-deleteContextMenuLi").on("click", function () {
            if (task != null) {
                $("#fwsjcjEx-contextMenuDiv").hide();
                deleteTask(task);
            }
        });
    }

    function fwsjcjCreateFWContextMenu(fwinfo) {
        //设置右键菜单点击事件
        $("#fwsjcj-BaseInfoContextMenuLi").off("click");
        $("#fwsjcj-BaseInfoContextMenuLi").on("click", function () {
            $("#fwsjcjFW-contextMenuDiv").hide();
            _viewFWInfo(fwinfo);
        });
    }

    function fwsjcjCreateContextMenu() {
        //设置右键菜单点击事件
        $("#fwsjcj-newTaskContextMenuLi").off("click");
        $("#fwsjcj-newTaskContextMenuLi").on("click", function () {
            if (PolygonPoints != null && PolygonPoints.length > 0) {
                $("#fwsjcj-contextMenuDiv").hide();
                var result = "";
                for (var i = 0; i < PolygonPoints.length; i += 2) {
                    result += PolygonPoints[i] + "," + PolygonPoints[i + 1] + ";";
                }
                /*var area =polygonTaskTool.getArea(PolygonPoints);
                debugger;*/
                result = result.substring(0, result.length - 1);
                /*//全国范围添加
                result=chinaRegionStr;
                var tpTool2 = new TPolygonTool(fwsjcjMap,null);
                tpTool2.open();
                var pointps=createChinaMapLine();
                debugger;
                var areas=tpTool2.getArea(pointps);
                taskAreas = areas/1000000;
                debugger;
                 */

                _viewTaskSet(0,result,null);
            }
        });
    }

    function initTMapEvent(str, map) {
        //str:#fwsjcj
        if (map != null) {
            //地图当前鼠标位置
            //注册鼠标在地图上的滑动事件
            TEvent.addListener(map, "mousemove", function (p) {
                //将像素坐标转换成经纬度坐标
                var lnglat = map.fromContainerPixelToLngLat(p);
                $(str + "-mousemoveLngLatDiv").html("鼠标位置经纬度：" + lnglat.getLng() + "," + lnglat.getLat());
            });
            //地图拖拽事件
            TEvent.addListener(map, "mousedrag", function () {
                //设置主界面图标文字
                initSetZoomDefaultPosition(false, str);
                $(str + "-contextMenuDiv").hide();
            });

            //地图缩放事件
            TEvent.addListener(map, "zoom", function () {
                //设置主界面图标文字
                initSetZoomDefaultPosition(false, str);
                var zoom = map.getZoom();
                if (str == "#fwsjcj") {

                    if (zoom > 4) {
                    } else if (zoom == 4) {
                        //地图缩放到权限位置
                        //fwsjcjZoomDefaultPosition();
                    }

                }

            });
        }

        return map;
    }

    function initSetSwitchMap(str, map) {

        var strSwitchMap = str + "-switchMap";
        $(strSwitchMap).children("span").html("卫星图");
        $(strSwitchMap).children("img").attr("title", "切换卫星地图");
        $(strSwitchMap).children("img").attr("src", "/static/app/fwsjcj/taskmanage/image/weixing-b.png");
        $(strSwitchMap).children("img").css("margin-left", "5px");
        //显示街道图
        map.setMapType(TMAP_NORMAL_MAP);
        //切换卫星图和街道图事件
        $(strSwitchMap).click(function () {
            taskLeftTitleSwitch();
            //切换地图功能控件
            if ($(strSwitchMap).children("img").attr("src") == "/static/app/fwsjcj/taskmanage/image/jiedao-b.png") {
                $(strSwitchMap).children("span").html("卫星图");
                $(strSwitchMap).children("img").attr("title", "切换卫星地图");
                $(strSwitchMap).children("img").attr("src", "/static/app/fwsjcj/taskmanage/image/weixing-b.png");
                $(strSwitchMap).children("img").css("margin-left", "5px");
                //显示街道图
                map.setMapType(TMAP_NORMAL_MAP);
            } else if ($(strSwitchMap).children("img").attr("src") == "/static/app/fwsjcj/taskmanage/image/weixing-b.png") {
                $(strSwitchMap).children("span").html("街道图");
                $(strSwitchMap).children("img").attr("title", "切换街道地图");
                $(strSwitchMap).children("img").attr("src", "/static/app/fwsjcj/taskmanage/image/jiedao-b.png");
                //显示卫星图
                map.setMapType(TMAP_HYBRID_MAP);//卫星图（有标注）
            }
        });
    }

//初始化卫星和街道地图及切换地图事件
    function initTMap(map, strMap, str) {
        //strMap:fwsjcj-map
        try {
            //判断是否连接上地图API
            var temp = TLocalSearch();
            var config = {
                projection: "EPSG:4326"
            }
            //初始化地图对象
            map=new TMap(strMap,config);
            //初始化地图
            //map = new TMap(strMap);
            //清空地图
            map.clearOverLays();
            //地图中心坐标--全国
            //var LonLat  = [109.2, 34.4];
            //var LonLat = [113.2, 25.4];
            var LonLat  = [103.2, 34.4];
            //地图缩放级别
            var zoom = 4;
            map.centerAndZoom(new TLngLat(LonLat[0], LonLat[1]), zoom);
            map.enableDoubleClickZoom();
            map.enableHandleMouseScroll();

            //创建比例尺控件对象
            var scale = new TScaleControl();
            //添加比例尺控件
            map.addControl(scale);

            //鹰眼
            var configOverView = {
                anchor: "TMAP_ANCHOR_BOTTOM_RIGHT",	//设置鹰眼位置,"TMAP_ANCHOR_TOP_LEFT"表示左上，"TMAP_ANCHOR_TOP_RIGHT"表示右上，"TMAP_ANCHOR_BOTTOM_LEFT"表示左下，"TMAP_ANCHOR_BOTTOM_RIGHT"表示右下，"TMAP_ANCHOR_LEFT"表示左边，"TMAP_ANCHOR_TOP"表示上边，"TMAP_ANCHOR_RIGHT"表示右边，"TMAP_ANCHOR_BOTTOM"表示下边，"TMAP_ANCHOR_OFFSET"表示自定义位置,默认值为"TMAP_ANCHOR_BOTTOM_RIGHT"
                size: new TSize(180, 120),			//鹰眼显示的大小
                isOpen: true						//鹰眼是否打开，true表示打开，false表示关闭，默认为关闭
            };
            //创建鹰眼控件对象
            var overviewMap = new TOverviewMapControl(configOverView);
            //添加鹰眼控件
            map.addControl(overviewMap);

            //地图缩放平移控件
            var config = {
                type: "TMAP_NAVIGATION_CONTROL_ZOOM",	//缩放平移的显示类型
                anchor: "TMAP_ANCHOR_TOP_RIGHT",			//缩放平移控件显示的位置
                offset: [0, 0],							//缩放平移控件的偏移值
                showZoomInfo: false						//是否显示级别提示信息，true表示显示，false表示隐藏。
            };
            //创建缩放平移控件对象
            var navigationControl = new TNavigationControl(config);
            //添加缩放平移控件
            map.addControl(navigationControl);

            //设置地图显示级别
            map.setZoomLevels([4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]);

            //切换卫星和街道图
            initSetSwitchMap(str, map);

            //设置控件显示
            $(str + "-setDivPosition").show();//显示工具条控件
            $(str + "-mousemoveLngLatDiv").show();//显示鼠标经纬度信息栏
            $(str + "-tabControlDiv").show();//显示侧边栏

            //全屏设置功能
            //initTMapSetFullScreen();

        } catch (e) {
            map = null;
        }

        return map;
    }

//信息窗样式设置
    function initAlert(str, data, bool) {
        if (data == "没有选择区域" || data == "初始化成功！" || data == "初始化失败！" || data == "地图初始化失败！" || data == "查询成功！" || data == "查询失败！" || data == "无查询结果！" || data == "处理成功！" || data == "处理失败！" || data == "设置成功！" || data == "设置失败！" || data == "保存成功！" || data == "删除成功！" || data == "清空成功！" || data == "线路规划成功！") {
            $(str + "-stopQueryDiv").hide();
        }
        //设置DIV层叠顺序为最大
        initSetDivZindexMax(str + "-infosDiv");
        //消息提示图标
        if (bool) {//成功
            $(str + "-infosDiv img").attr("src", "/static/app/fwsjcj/taskmanage/image/succes.png");
        } else {//失败
            $(str + "-infosDiv img").attr("src", "/static/app/fwsjcj/taskmanage/image/error.png");
        }
        $(str + "-infosDiv img").css("top","70px");
        $(str + "-infosDiv img").css("left","60px");
        //设置信息框显示文字
        $(str + "-infosDiv p").html(data);
        $(str + "-infosDiv").show();
        //消息提示3秒后自动关闭
        setTimeout(function () {
            $(str + "-infosDiv").hide();
        }, 3000);
    }

    var deleteTask = function(task) {
        if(task.state == '2'){
            Util.alert("只有任务状态为未分配和进行中的才能删除!");
            return;
        }

        Util.confirm("是否要删除选中的任务?", function() {
            $.ajax({
                url: getServer() + "/fw/task/"+task.id,
                type: "post",
                success: function (data) {
                    //fwsjcjInitAll();
                    initOperateData();
                }
            });
        }, function() {
            return;
        });

    };

    window._viewTaskSet = function(type,result,task) {
        debugger;
        console.log(JSON.stringify(result))
		//debugger;
        var slidebar = Util.slidebar({
            url: TaskSetFormHtml,
            width: "800px",
            cache: false,
            close : true,

            afterOpen: function () {
                createGroupGrid(task);
            },
            afterLoad: function () {
                initdivision();
                Tool.deserialize("taskSetForm", JSON.stringify(result));
                if(type==0){
                    getTaskNum();
                    $("#taskTypeForm").val(0);
                    $("#taskStatusForm").val(0);
                    $("#taskAreaarea").val(taskAreas);//task.points
                }else if(type==1){
                    $("#taskNameForm").val(task.name);
                    $("#taskTypeForm").val(task.type);
                    $("#taskStatusForm").val(task.state);
                    $("#taskGroup").val(task.groupName);
                    $("#groupId").val(task.groupId);
                    $("#taskNum").val(task.taskNum);
                    $("#taskId").val(task.id);
                    $("#groupLeaderId").val(task.groupLeaderId);
                    $("#groupLeader").val(task.groupLeader);
                    $("#taskAreaarea").val(task.area);//task.points
                    $("#division").val(task.division);//task.points
                }

                $("#taskArea").val(result);

                $("#saveBtn").on("click", function () {
                    if ($("#taskSetForm").valid()) {
                        //val.replace(/(^\s*)|(\s*$)/g, '');
                        var taskNum=$("#taskNum").val().replace(/(^\s*)|(\s*$)/g, '');
                        if(taskNum == '' || taskNum == undefined || taskNum == null){
                            Util.alert("任务编码不能为空!");
                            return;
                        }
                        var taskName=$("#taskNameForm").val().replace(/(^\s*)|(\s*$)/g, '');
                        if(taskName == '' || taskName == undefined || taskName == null){
                            Util.alert("任务名称不能为空!");
                            return;
                        }
                        var division=$("#division").val().replace(/(^\s*)|(\s*$)/g, '');
                        if(division == '' || division == undefined || division == null){
                            Util.alert("任务地区不能为空!");
                            return;
                        }
                        var data;
                        if(type==0) {
                             data = {
                                taskId: $("#taskId").val(),
                                taskNum: $("#taskNum").val(),
                                taskName: $("#taskNameForm").val(),
                                taskType: $("#taskTypeForm").val(),
                                //state:$("#taskTypeForm").val(),
                                groupName: $("#taskGroup").val(),
                                groupId: $("#groupId").val(),
                                groupLeaderId: $("#groupLeaderId").val(),
                                groupLeader: $("#groupLeader").val(),
                                mapInfo: $("#taskArea").val(),
                                taskArea:$("#taskAreaarea").val(),
                                 division:$("#divisionId").val(),
                            }
                        }else if(type==1){
                            data = {
                                taskId: $("#taskId").val(),
                                taskNum: $("#taskNum").val(),
                                taskName: $("#taskNameForm").val(),
                                taskType: $("#taskTypeForm").val(),
                                groupName: $("#taskGroup").val(),
                                groupId: $("#groupId").val(),
                                groupLeaderId: $("#groupLeaderId").val(),
                                groupLeader: $("#groupLeader").val(),
                                mapInfo: $("#taskArea").val(),
                                taskArea:$("#taskAreaarea").val(),
                                division:$("#divisionId").val(),
                            }
                        }
                        var url=getServer() + "/fw/task/save";

                        $.ajax({
                            url: url,
                            contentType:"application/json",
                            data: JSON.stringify(data),
                            type: "post",
                            success: function (data) {
                                Util.alert(data.message);
                                slidebar.close();
                                //PolygonPoints=[];
                                //PolygonPoints.length=0;
                                //fwsjcjInitAll();
                                initOperateData();
                            }
                        });
                    }
                });
            }
        });
    };
	var getTaskNum = function(){
		
		$.ajax({
			url: getServer() + "/fw/task/taskCode",
			type: "get",
			success: function (data) {
				$("#taskNum").val(data);
			}
		});
	};
    var createGroupGrid = function (task) {
        $("#groupGrid").datagrid({
            url: getServer() + "/fw/group/page?groupType=0",
            method: "get",
            dataType: "json",
            columns: [[
                {
                    field: 'groupName',
                    title: '小组名称',
                    width: '20%',
                    align: 'center',
                },
                {
                    field: 'groupLeader',
                    title: '小组组长',
                    width: '15%',
                    align: 'center'
                },
                {
                    field: 'groupMember',
                    title: '小组成员',
                    width: '27.65%',
                    align: 'center'
                },
                {
                    field: 'address',
                    title: '小组地区',
                    width: '25%',
                    align: 'center'
                },
                {field:'operation',title:'操作',width:'15%',align:'center',
                    formatter: function (value, task) {
                        return '<a onclick="_allot(\'' + task.groupName+ '\',\'' + task.id+ '\',\'' + task.groupLeaderId+ '\',\'' + task.groupLeader+ '\')">分配</a >';
                    }
                }
            ]],
            pagination: true,
            pageSize: 10
        });
    };
    window._allot = function (groupName,groupId,groupLeaderId,groupLeader) {
        //debugger;
        $("#taskGroup").val(groupName);
        $("#groupId").val(groupId);
        $("#groupLeaderId").val(groupLeaderId);
        $("#groupLeader").val(groupLeader);
    }

    window._viewFWInfo = function(fwinfo) {
        //console.log(JSON.stringify(fwinfo))
        //debugger;
        var slidebar = Util.slidebar({
            url: FWInfoFormHtml,
            width: "500px",
            cache: false,
            close : true,
            afterLoad: function () {
                Tool.deserialize("fwForm", JSON.stringify(fwinfo));
                $("#fwName").val(fwinfo.name);
                $("#belongTask").val(fwinfo.taskname);
                $("#fwLon").val(fwinfo.lon);
                $("#fwLat").val(fwinfo.lat);
                var fwType="城镇住宅";
                if(fwinfo.buildtype==1){
                    fwType="城镇非住宅";
                }else if(fwinfo.buildtype==0){
                    fwType="城镇住宅";
                }else if(fwinfo.buildtype==2){
                    fwType="农村住房";
                }else if(fwinfo.buildtype==3){
                    fwType="农村公共住宅";
                }
                $("#buildType").val(fwType);
                var spState="未提交";
                if(fwinfo.sptype==1){
                    spState="未审核";
                }else if(fwinfo.sptype==2){
                    spState="审核未通过";
                }else if(fwinfo.sptype==3){
                    spState="审核通过";
                }
                $("#spType").val(spState);
                $("#createTime").val(fwinfo.createtime);

                $("#viewFWInfoBtn").on("click", function () {
                    _viewFWDetailedInfo(fwinfo.formid,fwinfo.buildtype);
                    slidebar.close();
                });
            }
        });
    };

    // 房屋详细信息
    window._viewFWDetailedInfo = function(formId,category) {
        //debugger;
        // var censusDataHtml = sysPath + "/censusdata/views/censusForm_city.html";
        var censusDataHtml = openForm(category);
        var slidebar = Util.slidebar({
            url: censusDataHtml,
            width: "900px",
            cache: false,
            close : true,
            afterLoad: function () {
                getData(formId,category);
                $("#approved").on("click", function () {
                    $.ajax({
                        url: getZwController() + "/spForm",
                        data: JSON.stringify({
                            status: "3",
                            formId: formId,
                            buildingType: category
                        }),
                        type: "post",
                        contentType: "application/json; charset=utf-8",
                        success: function (data) {
                            debugger
                            initOperateData();
                            Util.alert(data.message);
                            slidebar.close();
                        }
                    });
                });
                $("#unapproved").on("click", function () {
                    debugger;
                    var reason = $('#unapproval_reason').css('display');
                    if (reason == 'none') {
                        $('#unapproval_reason').show();
                    } else {
                        formValidator();
                        if ($('#auditForm').valid()) {
                            var reason = $('#reason').val();
                            $.ajax({
                                url: getZwController() + "/spForm",
                                data: JSON.stringify({
                                    status: "2",
                                    reason: reason,
                                    formId: formId,
                                    buildingType: category
                                }),
                                type: "post",
                                contentType: "application/json; charset=utf-8",
                                success: function (data) {
                                    debugger
                                    $('#unapproval_reason').hide();
                                    initOperateData();
                                    Util.alert(data.message);
                                    slidebar.close();
                                }
                            });
                        }
                    }
                });
            }
        });
    };

    // 查看获取数据
    var getData = function(id,type){
        //debugger
        $.ajax({
            url: getZwController() + "/findById",
            data: {
                type: type,
                formId: id
            },
            type: "get",
            success: function (data) {
                Tool.deserialize("userForm", data);
                console.log(data);
                debugger
                // 若审批状态为未审核：1
                if (data.spType != "1"){
                    // 若为其他状态不显示审核按钮
                    $("#approved").hide();
                    $("#unapproved").hide();
                }
                if(data.image){
                    var imageURL=data.image.split(",");
                    console.log(imageURL);
                    var html="";
                    for (var i = 0; i <imageURL.length ; i++) {
                        html+='<div class="col-md-12 has-feedback" style="padding-top: 7px;display: flex;justify-content: center;">' +
                            '<img src='+imageURL[i]+' alt="暂无图片" id="+image[i]+" name="+image[i]+" style="width: 400px;height: 533px;border: 1px solid #ddd;"/>' +
                            '</div>'
                    }
                    $("#imgparent").append(html);
                }else {
                    var html="";
                    html+='<div class="col-md-12 has-feedback" style="padding-top: 7px;display: flex;justify-content: center;">' +
                        '<img src="" alt="暂无图片" id="image" name="image" style="width: 400px;height: 533px;border: 1px solid #ddd;"/>' +
                        '</div>'
                    $("#imgparent").append(html);
                }
                if (data.propertyNature == '其它') {
                    $('#propertyNatureOther').css('display', 'block');
                }
                if (data.housePurpose == '其它' || data.housePurpose == '其它集体公共建筑' || data.housePurpose == '开发性生产经营用房') {
                    $('#housePurposeOther').css('display', 'block');
                }
                if (data.structureType == '其它') {
                    $('#structureTypeOther').css('display', 'block');
                }
                /*if (data.image != '') {
                    $('#image').attr('src', data.image);
                }*/
                if (data.buildMethod == '其它') {
                    $('#buildMethodOther').css('display', 'block');
                }
                /* if(data.buildingIsDamage=='不确定'){
                     $('#buildingDamageRemark').css('display','block');
                 }*/
                //农村住宅辅助用房用途其它
                if (data.auxiliaryHousePurpose == '其它') {
                    $('#auxiliaryHousePurposeOther').css('display', 'block');
                }
                //农村住宅辅助用房构造方式其它
                if (data.auxiliaryHouseBuildMethod == '其它') {
                    $('#auxiliaryHouseBuildMethodOther').css('display', 'block');
                }
                //农村住宅居住房屋抗震构造措施情况其它
                if (data.houseMeasuresSituation == '其他抗震措施') {
                    $('#houseMeasuresSituationOther').css('display', 'block');
                }
                //农村住宅居住房屋抗震构造措施情况其它
                if (data.houseUseSituation == '其它') {
                    $('#houseUseSituationOther').css('display', 'block');
                }
                //农村住宅辅助用房结构其它
                if (data.auxiliaryHouseStructure == '其它') {
                    $('#auxiliaryHouseStructureOther').css('display', 'block');
                }
            }
        });
    }

    // 判断打开那个表单--查看表单
    var openForm = function(type){
        var openform;
        if(type == '1'){
            // 城镇住宅
            openform = sysPath + "/taskmanage/views/censusCheckForm_city.html";
        }else if(type == '0'){
            // 城镇非住宅
            openform = sysPath + "/taskmanage/views/censusCheckForm_uncity.html";
        }else if(type == '2'){
            // 农村住房
            openform = sysPath + "/taskmanage/views/censusCheckForm_rural.html";
        }else if(type == '3'){
            // 农村公共建筑
            openform = sysPath + "/taskmanage/views/censusCheckForm_publicrural.html";
        }
        return openform;
    }
    var initdivision = function () {
        var combotree = Combotree({
            // combo组件相关配置
            combo: {
                //要渲染的input的id
                id: "division",
                //下拉框的类型：单选or多选
                multi: false,
                key: {
                    idKey: "divisionId",
                    textKey: "divisionName"
                },
                //          placeholder: "请选择……",
                event: {
                    onShowPanel: function () {
                        console.log("show");
                    },
                    onHidePanel: function () {
                        console.log("hide");
                    },
                    onChange: function (n, o) {
                        debugger;
                        $("#divisionId").val(n[0].divisionId);
                        $("#division").val(n[0].divisionName);
                    }
                }
            },
            // ztree组件相关配置
            ztree: {
                data: {
                    simpleData: {
                        enable: true,
                        idKey: "divisionId", // 主键属性名
                        pIdKey: "parentId" // 父节点属性名
                    },
                    key: {
                        name: "divisionName" // 显示名称属性名
                    }
                },
                check: {
                    chkboxType: {
                        'Y': '',
                        'N': ''
                    }
                }
            },
            data: {
                url: getDivisionController() + 'initDivisionZtree',
                type: 'GET',
                success: function () {

                }
            },
            formatter: function (node) {
                node.open = false;
            },
            value: $("#divisionId").val(),
            search: {
                enable: true,
                fields: ["divisionName"]
            }
        });
    }

    return {
        fwsjcjInitAll:fwsjcjInitAll
    };
});